XSLT显式节点选择什么都不返回

时间:2010-12-08 15:13:11

标签: xslt

我无法让XSLT只返回XML中的类别值。为什么要返回lastupdate和path? ......我怎么能阻止这个?提前谢谢。

XML文档

<?xml version="1.0"?>
<categories count="3">
    <lastupdate>08/12/2010 12:27</lastupdate>
    <path>C:\</path>
    <category>Music</category>
    <category>News</category>
    <category>Sport</category>
</categories>

我的XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="categories">
        <html>
            <body>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tbody>
                        <tr>
                            <td>
                                <xsl:apply-templates/>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="category">
        <a>
            <xsl:value-of select="." />
        </a>
    </xsl:template>
</xsl:stylesheet>

输出HTML

<html>
    <body>
        <table border="0" cellpadding="0" cellspacing="0">
            <tbody>
                <tr>
                    <td>08/12/2010 12:27C:\
                        <a>Music</a>
                        <a>News</a>
                        <a>Sport</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

4 个答案:

答案 0 :(得分:3)

  

为什么lastupdate和path正在进行中   返回?

因为built-in rules,恰恰是这两个:

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

apply-templatesselect="node()"相同。然后,lastupdatepath元素通过元素的内置规则匹配(仅将模板应用于子节点),并且文本节点子节点通过文本节点的内置规则匹配(输出字符串)值)。

  

......我怎么能阻止这个?

覆盖其中一条内置规则,例如:

<xsl:template match="text()"/>

表示没有文本节点输出。或者使用像

这样的推送式方法
<xsl:template match="categories">
 <html>
  <body>
   <xsl:apply-templates select="category"/>
  </body>
 </html>
</xsl:template>  

答案 1 :(得分:1)

您的&lt; xsl:apply-templates /&gt;将所有匹配的模板应用于所有子节点。

由于您尚未为lastupdate和path定义匹配模板,因此XSLT会应用它的默认模板,在这种情况下会复制文本内容。

如果要禁用此功能,则必须覆盖默认模板(通常不太好)或在要处理的节点上限制模板应用程序。在您的示例中,将apply-templates扩展为

<xsl:apply-templates select="./category"/>

答案 2 :(得分:1)

在您的代码中,您正在为所有categories个子元素节点应用模板。见http://www.w3.org/TR/xslt#built-in-rule

因此,您需要以下代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="categories">
        <html>
            <body>
                <xsl:apply-templates select="category"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="category">
        <a>
            <xsl:value-of select="." />
        </a>
    </xsl:template>
</xsl:stylesheet>

获得所需的输出:

<html>
    <body>
        <a>Music</a>
        <a>News</a>
        <a>Sport</a>
    </body>
</html>

答案 3 :(得分:0)

可能是你在xslt底部的某个地方有这样的东西:

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

这可以解释为什么你在输出的中间某处得到这些值。