[xsl] Using XPath in an xsl:param?

Subject: [xsl] Using XPath in an xsl:param?
From: "Kevin Daniels" <KevinDaniels@xxxxxxxxxxx>
Date: Mon, 19 Jul 2004 01:02:54 -0400
I am using client-side transformations to filter XML in IE 6 (using MSXML)
and have run into a question about using xsl:param to pass in a list of
string values.  As an example, here is some bogus XML ans XSL to illustrate
the issues I'm having:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<!-- Edited with XML Spy v4.2 -->
<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>When a man loves a woman</title>
  <artist>Percy Sledge</artist>
  <country>USA</country>
  <company>Atlantic</company>
  <price>8.70</price>
  <year>1987</year>
 </cd>
 <cd>
  <title>Big Willie style</title>
  <artist>Will Smith</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>9.90</price>
  <year>1997</year>
 </cd>
 <cd>
  <title>Private Dancer</title>
  <artist>Tina Turner</artist>
  <country>UK</country>
  <company>Capitol</company>
  <price>8.90</price>
  <year>1983</year>
 </cd>
 <cd>
  <title>Red</title>
  <artist>The Communards</artist>
  <country>UK</country>
  <company>London</company>
  <price>7.80</price>
  <year>1987</year>
 </cd>
</catalog>

This is transformed using the following stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:param name="test" select="artist='Bob Dylan' or artist='Tina Turner'"/>

<xsl:template match="catalog">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th align="left">Title</th>
        <th align="left">Artist</th>
      </tr>
      <xsl:apply-templates select="cd"/>
      <tr><td colspan="2"><xsl:value-of select="$test"/></td></tr>
    </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="cd"/>
<xsl:template match="cd[$test]">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
</xsl:template>

</xsl:stylesheet>

I need to be able to dynamically pass in the artist names to use in the
match="cd[$test]" template.  The first question is that the select statement
in the global param declaration seems to be interpreting the XPath statement
rather than treating it as a literal string.  It ends up being evaluated as
a boolean.  Is there any way to prevent this from happening and keep it as a
string value?  In addition, I get an error telling me that <xsl:template
match="cd[$test]"> does not support variables within the expression.  I'm
not sure if this is due to the interpretation of $test.
Is there any way to get around these issues?  Essentially I'm looking for a
way to pass in a dynamic array of variables and create the match=""
statement dynamically.  Is this possible by passing the values I need in as
a delimited string like <xsl:param name="test" select="Bob Dylan|Tina
Turner"/> and then splitting up the result?  Just to clarify - the values
can be any combination of artist/name and so it is not practical to create
separate stylesheets for each case.  Any help is much appreciated.

Kevin

Current Thread