Iterating through attributes using xslt

2019-05-10 05:07发布

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?

标签: xml xslt xpath
2条回答
三岁会撩人
2楼-- · 2019-05-10 05:52

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>
查看更多
女痞
3楼-- · 2019-05-10 06:02

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 
查看更多
登录 后发表回答