Click to See Complete Forum and Search --> : Effective use of <xsl:param>


fenterbug
09-02-2005, 09:31 AM
Okay, I can't even select a single node based on a param. Here's the XML:
<nonformulary>
<drug>
<brandname>Cozaar</brandname>
</drug>
</nonformulary

Here's the XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>
<xsl:param name="brandname"/>

<xsl:template match="/">

<xsl:element name="nonformulary">
<xsl:element name="item"><xsl:value-of select="$brandname"/></xsl:element>

<xsl:for-each select="/nonformulary/drug[brandname='$brandname']">
<xsl:element name = "drug">
<xsl:element name="brandname"><xsl:value-of select="brandname"/></xsl:element>
</xsl:element>
</xsl:for-each>

</xsl:element>
</xsl:template>

</xsl:stylesheet>

And finally, when passed a param of 'Cozaar', here's the resulting XML:
<?xml version="1.0" encoding="UTF-16"?><nonformulary><item>Cozaar</item></nonformulary>
The problem must be in my select for the xsl:for-each statement. If I have the value 'Cozaar' in my param (as seen in the new 'item' element), then how do I select all drugs that have a brandname of 'Cozaar'?

sheila
09-06-2005, 02:16 AM
I can see two problems:

1. You've enclosed $brandname in speechmarks in your select expression so the templates are literally looking for:

<brandname>$brandname</brandname>

Remove the speechmarks and they will look for a brandname that matches the value of $brandname.

2. You've started your select expression with a single oblique ("/").

If you're trying to find every drug called "Cozar" in the XML document then you can use an absolute location path ("//"):

<xsl:for-each select="//nonformulary/drug[brandname = $brandname]">


I'm not brilliant at constructing efficient path expresions so, please, someone correct me if I'm wrong.