我一直在尝试学习如何在 xslt 中编写代码,但目前我一直坚持如何在 xsl:apply-templates 标记周围使用条件测试。
这是我正在测试的 xml。
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<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>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
这是我的 xslt
<xsl:template match="/">
<xsl:apply-templates select="catalog/cd" />
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="artist" />
<br />
<xsl:apply-templates select="country" />
<br />
<xsl:if test="country != 'USA' and year != '1985'">
<xsl:apply-templates select="year" />
</xsl:if>
</p>
</xsl:template>
<xsl:template match="artist">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="country">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="year">
<xsl:value-of select="." />
</xsl:template>
这是我的输出:
Bob Dylan
USA
Bonnie Tyler
UK
1988
Dolly Parton
USA
这是我期待的输出:
Bob Dylan
USA
Bonnie Tyler
UK
1988
Dolly Parton
USA
1982
即使我只想在国家/地区的值为 USA 并且年份的值为 1985 时才删除年份,但每次国家/地区的值为 USA 时都会删除年份。有没有更好的方法可以使用应用模板?