为什么XSLT转换会丢弃所有标记并返回文本而不是XML

时间:2017-01-29 16:12:40

标签: xslt

我正在尝试一些XSLT示例。但在其中一些中,我只获取XML标记内的值。

例如,try this低于XSLT。

我的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="/">
    <root>
      <xsl:apply-templates select="//cd[country='UK']"/>
    </root>
  </xsl:template>

  <xsl:template match="country">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

我希望结果是XML,就像这样。

<root>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
  ...
  ...
</root>

但我得到的结果是文本,而不是XML。

  

隐藏自己的心脏邦妮泰勒英国哥伦比亚广播公司记录9.90 1988年仍然得到了   布鲁斯加里摩尔英国维珍记录10.20 1990一夜只有蜜蜂   英国Polydor 10.90 1998 Sylvias Mother Dr.Hook UK CBS 8.10 1973 Maggie   可能Rod Stewart英国Pickwick 8.50 1990为了好时光Kenny Rogers   英国Mucik Master 8.70 1995 Tupelo Honey Van Morrison英国Polydor 8.20   1971年最佳猫史蒂文斯英国岛8.90 1990年停止萨姆布朗   英国A和M 8.90 1988年间谍桥T`Pau英国警报器7.90 1987年私人   舞者蒂娜特纳英国国会大厦8.90 1983帕瓦罗蒂晚会音乐会卢西亚诺   Pavarotti UK DECCA 9.90 1991 Red The Communards UK London 7.80 1987

有人可以帮助我理解我在这里失踪的东西吗?

2 个答案:

答案 0 :(得分:1)

首先, XSLT Tryit Editor 并不完全符合,因为除了HTML转换之外的任何XSLT,它们的原始演示都会将文本呈现为可能是其Web应用程序的一部分。因此XML标签不会显示标记。

尝试使用另一个在线XSLT小提琴引擎,该引擎显示当前输出,如下所示,仅返回<root><country>的节点。您的国家/地区模板仅定位一个节点,并且在将剩余节点转储为文本之前调用apply-templates,因为它们没有转换规则。此外,您冗余运行//以搜索当前上下文的所有子元素。

<?xml version="1.0" encoding="UTF-8"?><root>
    Hide your heart
    Bonnie Tyler
    <country>UK</country>
    CBS Records
    9.90
    1988

    Still got the blues
    Gary Moore
    <country>UK</country>
    Virgin records
    10.20
    1990

    One night only
    Bee Gees
    <country>UK</country>
    Polydor
    10.90
    1998

    Sylvias Mother
    Dr.Hook
    <country>UK</country>
    CBS
    8.10
    1973

    Maggie May
    Rod Stewart
    <country>UK</country>
    Pickwick
    8.50
    1990

    For the good times
    Kenny Rogers
    <country>UK</country>
    Mucik Master
    8.70
    1995
...
</root>

考虑以下调整后的XSLT,您指定<xsl:copy><xsl:copy-of select="*">以返回所有节点和文本。请参阅此在线xsltransform.net link的演示,反映出所需的结果:

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

  <xsl:template match="/catalog">
    <root>
       <xsl:apply-templates select="cd[country='UK']"/>
    </root>
  </xsl:template>

  <xsl:template match="cd">
    <xsl:copy>
       <xsl:copy-of select="*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

通过将模板应用于cd为英国的country元素,您开始了什么。

但是您没有匹配cd的模板 - 因此built-in template rule依次启用并将模板应用于cd所有子项。另一个内置模板规则复制作为这些元素的子节点的文本节点。

您应该更改第二个模板,使其与cd匹配 - 但事实是您的整个样式表可以简化为:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <root>
      <xsl:copy-of select="catalog/cd[country='UK']"/>
    </root>
  </xsl:template>
</xsl:stylesheet>
相关问题