2010-12-08 79 views
2

我无法让XSLT仅返回XML中的类别值。为什么lastupdate和路径被返回? ...我怎样才能阻止呢?提前致谢。XSLT显式节点选择无返回

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> 
+0

你的输出表明,这是不完整的XSLT your're运行。我怀疑你的xslt中有更多东西将`lastupdate`和`path`复制到你的输出中。 – Filburt 2010-12-08 15:20:52

+0

我同意。桌子从哪里来?从您发布的代码中不清楚 – 2010-12-08 15:22:39

+0

从他样本中的缩进来看,他将表格放在body和apply-templates之间,这也将解释输出。 – TToni 2010-12-08 15:25:37

回答

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

你< XSL:申请模板/>适用于所有匹配的模板所有子节点。

因为您尚未为lastupdate和路径定义匹配模板,所以XSLT会将其应用于默认模板,在这种情况下,将复制文本内容。

如果您想禁用此选项,您必须覆盖默认模板(通常不太好)或限制模板应用程序在您要处理的节点上。在你的榜样扩大应用模板来

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

它可能是你有这样的事情的地方在你的XSLT的底部:

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

这可以解释为什么你结束了这些值在你的输出中间的某个地方。

3

为什么lastupdate和路径是 返回?

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

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

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

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

...我该如何解决这个问题?

覆盖内置的一个规则中,如:

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

含义没有文本节点输出。或者使用推式的方法类似

<xsl:template match="categories"> 
<html> 
    <body> 
    <xsl:apply-templates select="category"/> 
    </body> 
</html> 
</xsl:template>