Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a Node as an XSL Template Parameter

Tags:

xslt

In an existing XSL stylesheet I was passing a string into a named template. Due to a requirements change, I now need to display four strings, the string I was displaying and its three siblings. Rather than pass the four separate strings into the named template, I'm trying to pass their parent element (PETrailingFund in the code below),

 <xsl:call-template name="row">
  <xsl:with-param name="label">Price/Earnings Trailing</xsl:with-param>
  <xsl:with-param name="formatMarker">x</xsl:with-param>
  <xsl:with-param name="fund" select="PETrailingFund" />
  <xsl:with-param name="benchmark">
    <xsl:value-of select="benchmark/PETrailingBenchmark"/>
  </xsl:with-param>
  <xsl:with-param name="benchmark2">
    <xsl:value-of select="benchmark2/PETrailingBenchmark"/>
  </xsl:with-param>
</xsl:call-template>

but the transformation blows up when I try to work with the "fund" parameter in the named template:

<xsl:template name="row">
  <xsl:param name="label" />
  <xsl:param name="fund" />
  <xsl:param name="benchmark">NOT PROVIDED</xsl:param>
  <xsl:param name="benchmark2">NOT PROVIDED</xsl:param>
  <xsl:param name="formatMarker">&#160;</xsl:param>
  <xsl:param name="useDecimalFormatter">yes</xsl:param>
  <tr>
    <td class="first popup">
      <xsl:value-of select="$label" disable-output-escaping="yes"/>
    </td>
    <td>
      <xsl:if test="$benchmark = 'NOT PROVIDED'">
        <xsl:attribute name="class">last</xsl:attribute>
      </xsl:if>
      <xsl:value-of select="$fund/child::*" />
      <xsl:value-of select="$formatMarker"/>
    </td>
  </tr>
</xsl:template>

From what I've read, I'm not really passing in a node but a result tree fragment and I need to get it back to a node (or node-set). Is that accurate or am I doing something else wrong? How would I convert it (I'm working in a fairly stock PHP5 environment that I can't change).

N.B., I trimmed a good bit of the named template for simplicity's sake.

like image 338
Tom Avatar asked Apr 14 '26 03:04

Tom


1 Answers

This is a nasty thing in XSLT 1.0. In 2.0 this happens automatically. In 1.0 you need a custom function, often called blah:node-set. It is available in many XSLT toolkits (I don't know yours or PHP). If you have access to EXSLT, it contains one too. Example: http://www.xml.com/pub/a/2003/07/16/nodeset.html

like image 169
Teun D Avatar answered Apr 21 '26 01:04

Teun D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!