Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through attributes using xslt

Tags:

xml

xslt

xpath

I have an xml data which has shown below.

<Roll NO="4620" CLASSNO="0" ID="0" DID="0" REVSN="0" DNO="3" ></Roll>
<Roll NO="4630" CLASSNO="0" ID="0" DID="0" REVSN="0" DNO="3"></Roll>

I want to iterate through the attributes without specifying the name using XSLT. Any way to do it?

like image 396
Tom Cruise Avatar asked Aug 28 '26 08:08

Tom Cruise


2 Answers

You can use this XPath @* to get all attributes, e.g.:

XML:

<Roll NO="4620" CLASSNO="0" ID="0" DID="0" REVSN="0" DNO="3"/>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/*">
        <xsl:for-each select="@*">
            <xsl:value-of select="concat(name(), ': ', ., ' ')"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output:

NO: 4620 CLASSNO: 0 ID: 0 DID: 0 REVSN: 0 DNO: 3 
like image 73
Kirill Polishchuk Avatar answered Aug 29 '26 20:08

Kirill Polishchuk


Your code is not well-formed xml, but as for the question surrounding it:

<xsl:template match="Roll"> <!-- or "SB" or whatever -->
  <xsl:for-each select="@*">
    <!-- ... whatever you want to do with them ... -->
    <!-- use local-name and value(.) -->
  </xsl:for-each>
</xsl:template>
like image 38
Christopher Creutzig Avatar answered Aug 29 '26 22:08

Christopher Creutzig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!