Page 1 of 1

problem passing a value using with-param

Posted: Tue Feb 05, 2008 11:33 pm
by tsh138
New to XSLT and here's an example of what I'm trying to do. My problem is that the value of foo is not getting passed from parsefoo to parser. I've tried some other ways, but nothing seems to work...any ideas? Like I said, I'm new to XSLT so if you see any other problems with the sample feel free to point them out. Thanks in advance.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="nodes" select="/root/node"/>

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"></xsl:call-template>
</xsl:variable>
</xsl:template>

<xsl:template name="parsefoo">
<xsl:for-each select="$nodes">
<xsl:call-template name="parser">
<xsl:with-param name="val"><xsl:value-of select="foo"/></xsl:with-param>
<xsl:with-param name="delimiter">" "</xsl:with-param>
<xsl:with-param name="elementName">"item"</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="parser">
<xsl:param name="val"/>
<xsl:param name="delimiter"/>
<xsl:param name="elementName"/>
<xsl:for-each select="tokenize($val,$delimiter)">
<xsl:element name="{$elementName}">
<xsl:value-of select="current()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 12:46 pm
by sorin_ristache
Hello,

The template parsefoo is not called because the variable foos is not used. If you declare a variable but you do not use it the XSLT processor may not evaluate the variable. Saxon 9 works in this way on your XSLT 2.0 stylesheet. For example if you replace the first template with

Code: Select all

    <xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<xsl:value-of select="$foos"/>
</xsl:template>
the value of element foo is passed correctly.


Regards,
Sorin

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 5:04 pm
by tsh138
That doesn't appear to have any effect I ran it through the XSLT debugger in Oxygen, using Saxon 9B, and still no value in 'val'. I must be doing something else wrong. Let me know if you see anything else. Here's the updated code.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="nodes" select="/root/node"/>

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
</xsl:template>

<xsl:template name="parsefoo">
<xsl:for-each select="$nodes">
<xsl:call-template name="parser">
<xsl:with-param name="val"><xsl:value-of select="foo"/></xsl:with-param>
<xsl:with-param name="delimiter">" "</xsl:with-param>
<xsl:with-param name="elementName">"item"</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="parser">
<xsl:param name="val"/>
<xsl:param name="delimiter"/>
<xsl:param name="elementName"/>
<xsl:for-each select="tokenize($val,$delimiter)">
<xsl:element name="{$elementName}">
<xsl:value-of select="current()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 5:13 pm
by sorin_ristache
I see that you did not change the XSLT stylesheet as I specified in the previous post. Please add a reference to the foos variable for evaluating it. For example replace

Code: Select all

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
</xsl:template>
with

Code: Select all

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<xsl:value-of select="$foos"/>
</xsl:template>
Regards,
Sorin

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 5:29 pm
by tsh138
I forgot to include that in my post, but it doesn't seem to have any affect.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="nodes" select="/root/node"/>

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<xsl:value-of select="$foos"/>
</xsl:template>

<xsl:template name="parsefoo">
<xsl:for-each select="$nodes">
<xsl:call-template name="parser">
<xsl:with-param name="val"><xsl:value-of select="foo"/></xsl:with-param>
<xsl:with-param name="delimiter">" "</xsl:with-param>
<xsl:with-param name="elementName">"item"</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="parser">
<xsl:param name="val"/>
<xsl:param name="delimiter"/>
<xsl:param name="elementName"/>
<xsl:for-each select="tokenize($val,$delimiter)">
<xsl:element name="{$elementName}">
<xsl:value-of select="current()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Here's some sample XML:

Code: Select all

<root>
<node>
<foo>test1</foo>
</node>
<node>
<foo>test2</foo>
</node>
<node>
<foo>test3</foo>
</node>
</root>

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 5:51 pm
by sorin_ristache
The name of a tag cannot contain the character ". You have to replace

Code: Select all

<xsl:with-param name="elementName">"item"</xsl:with-param>
with

Code: Select all

<xsl:with-param name="elementName">item</xsl:with-param>
For sending in the output the complete content of the foos variable including the markup you should replace

Code: Select all

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<xsl:value-of select="$foos"/>
</xsl:template>
with

Code: Select all

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<output>
<xsl:copy-of select="$foos"/>
</output>
</xsl:template>
With this template I get the following output from your 2 input files:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<output>
<item>test1</item>
<item>test2</item>
<item>test3</item>
</output>
Regards,
Sorin

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 6:00 pm
by tsh138
What is giving you that output. Running it through the XSLT debugger, using Saxan9B, I get this:

<?xml version="1.0" encoding="UTF-8"?>
<output/>

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 6:04 pm
by sorin_ristache
I get the following output both in the Editor perspective and in the XSLT Debugger perspective

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<output>
<item>test1</item>
<item>test2</item>
<item>test3</item>
</output>
with Saxon 9B applied to the following XML file and XSLT file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<root>
<node>
<foo>test1</foo>
</node>
<node>
<foo>test2</foo>
</node>
<node>
<foo>test3</foo>
</node>
</root>

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="nodes" select="/root/node"/>

<xsl:template match="/">
<xsl:variable name="foos">
<xsl:call-template name="parsefoo"/>
</xsl:variable>
<output>
<xsl:copy-of select="$foos"/>
</output>
</xsl:template>

<xsl:template name="parsefoo">
<xsl:for-each select="$nodes">
<xsl:call-template name="parser">
<xsl:with-param name="val"><xsl:value-of select="foo"/></xsl:with-param>
<xsl:with-param name="delimiter">" "</xsl:with-param>
<xsl:with-param name="elementName">item</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="parser">
<xsl:param name="val"/>
<xsl:param name="delimiter"/>
<xsl:param name="elementName"/>
<xsl:for-each select="tokenize($val,$delimiter)">
<xsl:element name="{$elementName}">
<xsl:value-of select="current()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Regards,
Sorin

Re: problem passing a value using with-param

Posted: Wed Feb 06, 2008 6:11 pm
by tsh138
I found my problem. Somehow the copy of the XSLT I was going through was missing the $ in front of val. Thanks for all of the help.