XSLT意外的重复文本

时间:2011-08-07 09:28:42

标签: xml xslt

我正在创建一个带有xslt的html页面,将我的xml格式化为一个html页面,但是我得到一个重复的值,我找不到原因,下面是我的xsl,xml和html。我已经指出了我的重复值直接发生在下面,感谢大家的帮助!

<fieldset>
    <legend>Joys of a MAD man</legend><ol> 
            Joys of a MAD man *********** Why is the title repeated? ************
           <li>Slow Moving<a href="./Music/Splintz - Joys of a MAD Man/Slow Moving.mp3"> 
          [Download]
          </a></li> 
</ol></fieldset> 

我的XML

<albums>
    <album>
        <title>Joys of a MAD man</title>
        <track>Slow Moving</track>
    </album>
    <album>
        <title>Single</title>
        <track>None</track>
    </album>
</albums>

最后是我的xsl

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="album">
        <fieldset>
            <legend><xsl:value-of select="title/text()" /></legend>
            <ol>
                <xsl:apply-templates />
            </ol>
        </fieldset>
    </xsl:template>

        <xsl:template match="track">        
           <li>
               <xsl:value-of select="text()" />
               <a>
                   <xsl:attribute name="href">
                   <xsl:text>./Music/Splintz - Joys of a MAD Man/</xsl:text>
                   <xsl:value-of select="text()"/>
                   <xsl:text>.mp3</xsl:text>
               </xsl:attribute>
               [Download]
               </a>
           </li>
         </xsl:template>
</xsl:stylesheet>

3 个答案:

答案 0 :(得分:2)

built-in default template rules文本复制到结果文档中。

<xsl:apply-templates/><xsl:apply-templates select="child::node()"/>的缩写。

您在xsl:apply-templates的模板匹配中使用了album。当您“站在”album元素上时,title是要处理的子节点之一。

匹配title的内置模板第二次输出了text()“MAD男子的欢乐”。

有多种方法可以防止第二次输出title文本。你可以:

  • 将与title匹配的空模板添加到样式表中,以防止内置模板匹配:
    • <xsl:template match="title"/>
  • title中排除apply-templates
    • <xsl:apply-templates select="node()[not(self::title)]"/>
  • 仅将模板应用于track子元素:
    • <xsl:apply-templates select="track"/>

答案 1 :(得分:1)

因为您将<xsl:apply-templates />应用于与上面的album相同的上下文名称value-of。它从所有后代中删除所有文本节点。 明确说出<xsl:apply-templates match="track" />

答案 2 :(得分:1)

正如其他人所指出的,默认的XSLT处理是选择执行内置的XSLT模板,这会导致输出文本节点。

由于您的代码不依赖于文本节点的XSLT内置模板,因此最简单的解决方案是使用没有正文的模板覆盖此模板。

将以下内容添加到您的代码中

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

现在您的完整代码已成为

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

    <xsl:template match="album">
        <fieldset>
            <legend>
                <xsl:value-of select="title/text()" />
            </legend>
            <ol>
                <xsl:apply-templates />
            </ol>
        </fieldset>
    </xsl:template>

    <xsl:template match="track">
        <li>
            <xsl:value-of select="text()" />
            <a>
                <xsl:attribute name="href">
                    <xsl:text>./Music/Splintz - Joys of a MAD Man/</xsl:text>
                    <xsl:value-of select="text()"/>
                    <xsl:text>.mp3</xsl:text>
                </xsl:attribute> 
                    [Download]               
            </a>
        </li>
    </xsl:template>

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

,结果不包含不需要的重复文字

<fieldset>
   <legend>Joys of a MAD man</legend>
   <ol>
      <li>Slow Moving<a href="./Music/Splintz - Joys of a MAD Man/Slow Moving.mp3">
                [Download]
            </a>
      </li>
   </ol>
</fieldset>
<fieldset>
   <legend>Single</legend>
   <ol>
      <li>None<a href="./Music/Splintz - Joys of a MAD Man/None.mp3">
                [Download]
            </a>
      </li>
   </ol>
</fieldset>
相关问题