下面这个例子可以帮助你很好的理解,注意将注释掉的部分一个一个去掉试试哦。
先去掉z的注释,然后去掉y的注释,然后去掉x的注释
1:首先是xml部分
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<a>
<b>
<x>
<y>
<z>
<item value="test1"/>
</z>
</y>
</x>
</b>
</a>
<b>
<a>
<a>
<x>
<y>
<z>
<item value="test2"/>
</z>
</y>
</x>
</a>
</a>
</b>
</root>
2:然后是xslt部分的代码(text.xsl)
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title>Test</title>
</head>
<body>
<ul><xsl:apply-templates select="/root/*"/></ul>
</body>
</html>
</xsl:template>
<xsl:template match="a|b|c">
<li>
<i>(<xsl:value-of select="name()"/>)</i>
<xsl:choose>
<xsl:when test="count(x/y/z)>0">
<xsl:apply-templates select="x/y/z"/>
</xsl:when>
</xsl:choose>
<ul><xsl:apply-templates select="*"/></ul>
</li>
</xsl:template>
<!-- <xsl:template match="x"> -->
<!-- Valuex -->
<!-- </xsl:template> -->
<xsl:template match="w">
Valuew
</xsl:template>
<!-- <xsl:template match="y"> -->
<!-- Valuey -->
<!-- </xsl:template> -->
<!-- <xsl:template match="z"> -->
<!-- Value: <xsl:value-of select="item/@value"/> -->
<!-- </xsl:template> -->
</xsl:transform>
最后输出
- (a)
- (b)
- (b)
- (a)
- (a)
- (a)