Search code examples
xmlxsltfxsl

Element selection using variable


Basically I have a small template that looks like:

<xsl:template name="templt">
    <xsl:param name="filter" />
    <xsl:variable name="numOrders" select="count(ORDERS/ORDER[$filter])" />
</xsl:template>

And I'm trying to call it using

<xsl:call-template name="templt">
    <xsl:with-param name="filter" select="PRICE &lt; 15" />
</xsl:call-template>

Unfortunately it seems to evaluate it before the template is called (So effectively "false" is being passed in) Enclosing it in quotes only makes it a string literal so that doesn't work either. Does anybody know if what I'm trying to achive is possible? If so could you shed some light on it? Cheers


Solution

  • how about the following:

    <?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" indent="yes"/>
    
      <xsl:template name="templt">
        <xsl:param name="filterNodeName" />
        <xsl:param name="filterValue" />
        <xsl:variable name="orders" select="ORDERS/ORDER/child::*[name() = $filterNodeName and number(text()) &lt; $filterValue]" />
        <xsl:for-each select="$orders">
          <xsl:value-of select="."/>
        </xsl:for-each>
      </xsl:template>
    
      <xsl:template match="/">
        <xsl:call-template name="templt">
          <xsl:with-param name="filterNodeName" select="'PRICE'" />
          <xsl:with-param name="filterValue" select="15" />
        </xsl:call-template>
      </xsl:template>
    </xsl:stylesheet>
    

    If you still want to use a single parameter only, you could tokenize in template 'templt' first.