【问题标题】:Basic XML/XSLT : applying template to several elements基本 XML/XSLT:将模板应用于多个元素
【发布时间】:2014-07-11 11:46:46
【问题描述】:

我正在尝试自学 XSLT。这可能是一个非常基本的问题,但经过大量搜索,以及网络上的其他网站,我仍然无法解决这个问题。

我一直在尝试重新创建此问题中描述的场景:How can you deal with embedded XML tags in XSLT?

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
  <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>

这是 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>

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

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

 <xsl:template match="favoriteMovie">
  <li><xsl:copy-of select="node()"/></li>
 </xsl:template>
</xsl:stylesheet>

这适用于这个特定的示例。但是当我添加另一个“最喜欢的电影”时......

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
  <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>
<favoriteMovies>
  <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
</favoriteMovies>

我得到的只是一个错误......

XML 解析错误:文档元素位置后出现垃圾: localhost:8888/test.xml 第 6 行,第 1 列: ^

我需要进行哪些更改才能使其正常工作?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    任何 XML 文档都需要有一个包含所有其他元素的顶级根元素,因此您需要例如

    <root>
    <favoriteMovies>
      <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
    </favoriteMovies>
    <favoriteMovies>
      <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
    </favoriteMovies>
    </root>
    

    <favoriteMovies>
     <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
     <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
    <favoriteMovies>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2012-01-12
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 2021-08-12
      相关资源
      最近更新 更多