我正在为一个项目玩XSLT,并且只是尝试使用一些XSLT函数来获得所需的输出并遇到了一个我要解决的小问题。我见过很多关于更换的文章,但到目前为止,都没有一篇能解决这个看似简单的问题。
给出此XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country><font size='8pt' name='Verdana' color='navy'/>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
我正在尝试获得预期的输出(不包括美国):
<div>
<b>Empire Burlesque</b>
<p>
<i>
<a href="wwww.google.com.au">GOOGLE </a>
</i>
</p>
</div>
但是相反,我得到的是下面的文字“ USA”。
<div>
<b>Empire Burlesque</b>
<p>
<i>
<a href="wwww.google.com.au">GOOGLE </a>
</i>USA
</p>
</div>
这是我写的XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<div>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="country"/>
</div>
</xsl:template>
<xsl:template match="country">
<p>
<xsl:call-template name="replacement">
<xsl:with-param name="txt" select="."/>
</xsl:call-template>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template name="replacement">
<i><a href="wwww.google.com.au">GOOGLE </a> </i>
</xsl:template>
<xsl:template match="title">
<b><xsl:value-of select="."/></b>
</xsl:template>
</xsl:stylesheet>
有人可以告诉我一些如何获得所需输出的提示,而不会遇到country元素的美国文本吗?
干杯
答案 0 :(得分:1)
您的模板匹配country
包含以下说明:
<xsl:apply-templates/>
这会将模板应用于country
的所有子节点-包括文本节点"USA"
。您没有匹配text()
的模板,因此built-in template rule会将其复制到输出中。{p>
如果您不希望发生这种情况,请删除xsl:apply-templates
指令(或添加模板以其他方式处理文本节点)。