Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional test around xsl:apply-templates
    primarykey
    data
    text
    <p>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.</p> <p>Here's the xml that I am testing.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;catalog&gt; &lt;cd&gt; &lt;title&gt;Empire Burlesque&lt;/title&gt; &lt;artist&gt;Bob Dylan&lt;/artist&gt; &lt;country&gt;USA&lt;/country&gt; &lt;company&gt;Columbia&lt;/company&gt; &lt;price&gt;10.90&lt;/price&gt; &lt;year&gt;1985&lt;/year&gt; &lt;/cd&gt; &lt;cd&gt; &lt;title&gt;Hide your heart&lt;/title&gt; &lt;artist&gt;Bonnie Tyler&lt;/artist&gt; &lt;country&gt;UK&lt;/country&gt; &lt;company&gt;CBS Records&lt;/company&gt; &lt;price&gt;9.90&lt;/price&gt; &lt;year&gt;1988&lt;/year&gt; &lt;/cd&gt; &lt;cd&gt; &lt;title&gt;Greatest Hits&lt;/title&gt; &lt;artist&gt;Dolly Parton&lt;/artist&gt; &lt;country&gt;USA&lt;/country&gt; &lt;company&gt;RCA&lt;/company&gt; &lt;price&gt;9.90&lt;/price&gt; &lt;year&gt;1982&lt;/year&gt; &lt;/cd&gt; </code></pre> <p></p> <p>Here is my xslt</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:apply-templates select="catalog/cd" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="cd"&gt; &lt;p&gt; &lt;xsl:apply-templates select="artist" /&gt; &lt;br /&gt; &lt;xsl:apply-templates select="country" /&gt; &lt;br /&gt; &lt;xsl:if test="country != 'USA' and year != '1985'"&gt; &lt;xsl:apply-templates select="year" /&gt; &lt;/xsl:if&gt; &lt;/p&gt; &lt;/xsl:template&gt; &lt;xsl:template match="artist"&gt; &lt;xsl:value-of select="." /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="country"&gt; &lt;xsl:value-of select="." /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="year"&gt; &lt;xsl:value-of select="." /&gt; &lt;/xsl:template&gt; </code></pre> <p>Here is my output:</p> <pre><code>Bob Dylan USA Bonnie Tyler UK 1988 Dolly Parton USA </code></pre> <p>Here is the output I was expecting:</p> <pre><code>Bob Dylan USA Bonnie Tyler UK 1988 Dolly Parton USA 1982 </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload