[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 -->
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <title>When a man loves a woman</title>
  <artist>Percy Sledge</artist>
  <title>Big Willie style</title>
  <artist>Will Smith</artist>
  <title>Private Dancer</title>
  <artist>Tina Turner</artist>
  <artist>The Communards</artist>

This is transformed using the following stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

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

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

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


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.


Current Thread