Reputation: 21
//I need to make it so only CDs with prices less than 25 will appear on the page. However I am restricted to using the apply-templates command and cannot use the for-each. NOTE: I cannot use the for-each as my demonstrator wants to assess our ability to use the apply templates command
<?xml version = "1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="show.xsl"?>
<cdstore>
<cd>
<title>The bests</title>
<price>23.90 </price>
<year>2005 </year>
</cd>
<cd>
<title>The leasts</title>
<price>29.90 </price>
<year>2008 </year>
</cd>
<cd>
<title>The middles</title>
<price>15.90 </price>
<year>2011 </year>
</cd>
</cdstore>
<!--
<?xml version ="1.1" encoding = "ISO-8859-1"?>
<xml: stylesheet version = "1.0" xmlns:xsl="http://www.w3.org">
<xsl: template match = "cdstore">
<html>
<body>
<h3> CDs sale </h3>
<xsl:apply-templates/>
</body>
</html>
<xsl:template match="cd">
<p>
<xsl:apply-templates select = "title"/>
<xsl:apply-templates select = "year"/>
</p>
</xsl:template>
<xsl:template match = "title">
Title: <span><xsl:value-of select="."/></span><br/>
</xsl:template>
<xsl:template match = "year">
Year: <span><xsl:value-of select="."/></span><br />
</xsl:template>
</xsl:stylesheet>
-->
//How can I use the xsd:apply templates command to make it so only the titles and years appear for costs less than 25? WITHOUT using the for-each loop. I'm not sure if I put the commands in the apply-templates or a part of the value-of. Any help is greatly appreciated
Upvotes: 1
Views: 59
Reputation: 122404
You can do either - you could change the apply-templates
to restrict which elements you're applying templates to:
<h3> CDs sale </h3>
<xsl:apply-templates select="cd[price < 25]"/>
Or alternatively you could leave it applying templates to all the cd
elements but add a second template alongside the existing match="cd"
one to match the elements you don't want, and ignore them
<xsl:template match="cd[price >= 25]" />
When you have two different templates that could match the same node, the one that is chosen depends on specific rules in the XSLT specification, but essentially a match pattern of "elementname[predicate]"
will always win over one matching just "elementname"
, which in turn will win over one matching "*"
or "node()"
.
Upvotes: 1