You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by St...@adc.com on 2001/01/25 07:44:48 UTC

expression parameters to stylesheets


Hi xalan-dev,

I'm trying to write a transformation that will accept an expression as a
parameter so as to filter the input XML in a flexible way. The following
examples show my experimentation.

The input XML like this:

<?xml version="1.0"?>
<address-book>
     <address>
          <first-name>Fred</first-name>
          <last-name>Flintstone</last-name>
          <zip>55555</zip>
          <country>unknown</country>
     </address>
     <address>
          <first-name>Steven</first-name>
          <second-name>Shaw</second-name>
          <zip>4152</zip>
          <country>Australia</country>
     </address>
</address-book>

This transformation works as expected:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="address-book">
    <xsl:copy>
      <xsl:apply-templates select="address[country='Australia']"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The following parameterised transformation works as expected:

<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:param name="country">unknown</xsl:param>

  <xsl:template match="address-book">
    <xsl:copy>
      <xsl:apply-templates select="address[country=$country]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

The following parameterised transformation does not work:

<?xml version="1.0"?>

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:param name="expr">country=Australia</xsl:param>

  <xsl:template match="address-book">
    <xsl:copy>
      <xsl:apply-templates select="address[$expr]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

Can someone tell me how to get something like this to work? There are similar
restrictions for the values of a template's match attribute and a sort's select
attribute. Anyone point to to the place in the spec that explains these
restrictions?

The only other solution I can think of would be to build or generate a bunch of
transformations one for each kind of filter...

Steve.