Iterating through attributes using xslt
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?
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
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>
精彩评论