I've been trying to learn how to code in xslt and currently am stuck on how to use conditional tests around the xsl:apply-templates tag.
Here's the xml that I am testing.
<?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>
Here is my 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>
Here is my output:
Bob Dylan
USA
Bonnie Tyler
UK
1988
Dolly Parton
USA
Here is the output I was expecting:
Bob Dylan
USA
Bonnie Tyler
UK
1988
Dolly Parton
USA
1982
Even though I want to remove the year only when country has a value of USA and year has a value of 1985 it is removing the year every time country has a value of USA only. Is there a better way I can use apply-templates?