6

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?

3
  • 1
    While this can be done, bear in mind that the order of attributes is undefined and different XSLT processors can traverse and output the results of processing each attribute in different order. This means that the output is unpredictable, implementation-dependent and any solution that "iterates" through attributes isn't portable. One solution to this problem is to use <xsl:sort> and process the attributes sorted by name. This would be portable. On the other hand, it may not be possible with all XSLT processors to process the attributes in their "visible order". Commented Sep 13, 2011 at 4:35
  • @Dimitre: That's only a problem if the order is important in the output; this isn't specified in the question. Just out of curiosity, do you know of ANY xslt processor that doesn't process them in the 'visible order'? I've yet to find one that doesn't. Commented Sep 13, 2011 at 6:49
  • @Flynn1179: I think that the order isn't determined by the XSLT processor at all, but by the XML parser that uses. I haven't made any investigations in what order different XML parsers present attributes, but wouldn't (shouldn't) be surprized if some XML parsers produce them not in the "visible" order -- as this is not mandated by the XML specification. Here is an example -- a default attribute from a DTD/Schema that isn't "visible" in the text of the XML document. At which position in the "ordered" sequence of attributes should the default attribute be? A parser may decide as it likes! Commented Sep 13, 2011 at 12:15

2 Answers 2

17

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 
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.