Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call xslt template with parameter

Tags:

xslt

I have this xslt:

  <xsl:template name="dumpDebugData">
    <xsl:param name="elementToDump" />
    <xsl:for-each select="$elementToDump/@*">
      <xsl:text>&#10;</xsl:text>    <!-- newline char -->
      <xsl:value-of select="name()" /> : <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>

i want to display every element (as in name/value), how do i call this template?

like image 453
raklos Avatar asked Jun 05 '09 17:06

raklos


People also ask

How do you call a template in XSLT?

By invoke, we mean that the named template is called and applied to the source document. If a template does not have a name, it cannot be called by this element. The xsl:template element is used to create a template. You can name a template by using the name attribute of the xsl:template element.

How do you use parameters in XSLT?

To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet. Pass the XsltArgumentList object to the Transform method.

How do you pass a variable in an xsl template?

You are applying templates selecting folders , but have a template matching on folder . Either change it to folder , or if you have a folders template make sure that it passes the var1 parameter value down to the folder template. Your with-param @select uses '{var}' , which selects that literal string {var} .

How do I pass multiple parameters in XSLT?

Answer. In order to pass multiple parameters to XSLT from BP you'll need to pass '=' as a separator.


3 Answers

Since the template expects a node set, you must do:

<xsl:call-template name="dumpDebugData">
  <xsl:with-param name="elementToDump" select="some/xpath" />
</xsl:call-template>
like image 177
Tomalak Avatar answered Sep 26 '22 14:09

Tomalak


Try something like this:

<xsl:call-template name="dumpDebugData">
    <xsl:with-param name="elementToDump">foo</xsl:with-param>
</xsl:call-template>
like image 32
Andrew Hare Avatar answered Sep 24 '22 14:09

Andrew Hare


The original answer does not use the parameter. It only works if the paramater = the current element. This takes the parameter into account.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output indent="yes" />
   <xsl:strip-space elements="*" />
   <xsl:template match="element()">
      <xsl:call-template name="dumpDebugData">
         <xsl:with-param name="elementToDump" select="." />
      </xsl:call-template>
      <xsl:apply-templates />
   </xsl:template>
   <xsl:template name="dumpDebugData">
      <xsl:param name="elementToDump" />
      Node:
      <xsl:value-of select="name($elementToDump)" />
      :
      <xsl:value-of select="text($elementToDump)" />
      <xsl:for-each select="$elementToDump/@*">
         Attribute:
         <xsl:value-of select="name()" />
         :
         <xsl:value-of select="." />
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
like image 22
user2587355 Avatar answered Sep 26 '22 14:09

user2587355