Xml XSLT意外重复文本
我正在使用xslt创建一个html页面,以将xml格式化为html页面,但是我得到了一个重复的值,我找不到原因,下面是我的xsl、xml和html。我已经在下面直接指出了我的重复值出现的位置,谢谢大家的帮助Xml XSLT意外重复文本,xml,xslt,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
<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:apply templates
。当您“站”在相册
元素上时,标题
是要处理的子节点之一
内置模板匹配了标题
输出它的文本()
“疯子的快乐”第二次。
有许多方法可以防止第二次输出标题
文本。你可以:
- 将与
标题
匹配的空模板添加到样式表中,以防止内置模板匹配:
- 从
应用模板中排除标题
:
- 仅将模板应用于
轨迹
子元素:
正如其他人指出的,默认的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>
现在,您的完整代码如下所示:
<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>
并且结果不包含不需要的重复文本:
<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>
疯子的快乐
慢行
单身
没有
或者,为title元素添加一个空模板: