XSLT解析比我选择的更多的数据

Eri*_*ric 1 xml xslt xslt-1.0

我非常感谢任何帮助,理解为什么我从变换中获得的数据比我在样式表中实际选择的数据要多.所以这是我的XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<e:root xmlns:e="http://www.yahoo.com">
  <e:first>Hi</e:first>
  <e:cds>
    <e:cd>
      <e:title>Eric</e:title>
    </e:cd>
    <e:cd>
      <e:title>Tara</e:title>
    </e:cd>
  </e:cds>
</e:root>
Run Code Online (Sandbox Code Playgroud)

这是我的样式表:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:e="http://www.yahoo.com" version="1.0">
    <xsl:output method="xml" indent="yes" />

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

    <xsl:template match="e:cd">
      <xsl:element name="T"><xsl:value-of select="e:title"/></xsl:element>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这是输出:

<?xml version="1.0" encoding="UTF-8"?>
  Hi

    <T>Eric</T>
    <T>Tara</T>
Run Code Online (Sandbox Code Playgroud)

如何停止选择<e:first>节点的转换?我没有在变换中明确要求它,但它在我的结果中出现(带有'Hi'文本).我疯了,试图理解为什么.非常感谢您提前提供的任何帮助.

Dan*_*ley 5

这是因为XSLT的内置模板规则.

由于您xsl:value-ofe:cd模板中使用,因此您只需添加此模板即可:

<xsl:template match="text()"/>
Run Code Online (Sandbox Code Playgroud)

另一种选择是缩小您正在处理的范围:

<xsl:template match="/">
    <xsl:apply-templates select="e:root/e:cds"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

此外,除非您动态创建元素名称,否则没有理由使用xsl:element...

<T><xsl:value-of select="e:title"/></T>
Run Code Online (Sandbox Code Playgroud)