+ -
当前位置:首页 → 问答吧 → xsl递归问题

xsl递归问题

时间:2010-09-10

来源:互联网

XML代码如下:
XML code

<?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>



XSL代码如下:
XML code

<?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="z">
        Value: <xsl:value-of select="item/@value"/>
    </xsl:template>
</xsl:transform>



按我的理解出来的结果应该是这样的:
(a)
----(b) Value: test1
(b)
----(a)
--------(a) Value: test2


但实际出来的结果却是这样:
(a)
----(b) Value: test1
----------Value: test1
(b)
----(a)
--------(a) Value: test2
--------------Value: test2


我觉得应该是 "<ul><xsl:apply-templates select="*"/></ul>" 这一句中的select="*"调用了match="z"模板了,但是select="*"不是只选择a或者b或者c节点下的所有第一级子节点吗,为什么会选到了z子节点去了呢?

有人能帮忙解析一下吗?

我暂时的解决办法是改写为 "<ul><xsl:apply-templates select="a|b|c"/></ul>"

作者: nwleo   发布时间: 2010-09-10

XSLT有默认实现以下这段
  <xsl:template match="×">
  <xsl:apply-templates select="*"/>
  </xsl:template>

作者: cds27   发布时间: 2010-09-15