raklos
raklos

Reputation: 28545

call xslt template with parameter

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?

Upvotes: 20

Views: 71099

Answers (4)

user2587355
user2587355

Reputation: 83

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>

Upvotes: 7

samjudson
samjudson

Reputation: 56853

There are a number of issues in your original XSLT, so I worked through it and got you the following code which does what you want I believe:

<?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()" />
      :
      <xsl:value-of select="text()" />
      <xsl:for-each select="attribute::*">
         Attribute:
         <xsl:value-of select="name()" />
         :
         <xsl:value-of select="." />
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Tomalak
Tomalak

Reputation: 338108

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>

Upvotes: 35

Andrew Hare
Andrew Hare

Reputation: 351446

Try something like this:

<xsl:call-template name="dumpDebugData">
    <xsl:with-param name="elementToDump">foo</xsl:with-param>
</xsl:call-template>

Upvotes: 14

Related Questions