XSL遍历xml节点内容
xml.xml:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="xml.xsl"?>
<root>
<a>a
<a>aa
<a>aaa</a>
</a>
<a>aa</a>
</a>
<a>a
<a>aa</a>
</a>
</root>
xml.xsl:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="root/*">
<xsl:apply-templates select=".">
<xsl:with-param name="Num" select="position()"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="*">
<xsl:param name="Num"/>
<xsl:value-of select="$Num"/>
<xsl:value-of select="text()"/>
<br/>
<xsl:for-each select="*">
<xsl:apply-templates select=".">
<xsl:with-param name="Num">
<xsl:value-of select="$Num"/>.<xsl:value-of select="position()"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
效果:
1 a
1.1 aa
1.1.1 aaa
1.2 aa
2 a
2.1 aa
数字表示节点位置,x.x.x表示节点的层级。
相关链接:http://www.tuanpike.com/a/APACHE/20120407/736.html( jquery遍历xml )