Link to home
Start Free TrialLog in
Avatar of Jammerules
Jammerules

asked on

xslt "with-param" and "call-template" within the same file

Experts,

I am trying to understand how to use "with-param" and "call-template" concepts of xslt. I have a xsl file called "Sample.xsl" and below is the code in it.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     
	<xsl:template match="/">			 
                        <xsl:variable name="NSGNumber" select="number(10)"/> 
                        <xsl:message>NSGNumber::</xsl:message>			 
			<xsl:if test="$inputData='0'">
				<xsl:message>inputData is Zero (New)</xsl:message> 
				<xsl:call-template name="InputDataIsZero">
      				<xsl:with-param name="paramNSGNumber" select="$NSGNumber"/>
				</xsl:call-template>
			</xsl:if>  
	</xsl:template> 
	
	<xsl:template name="InputDataIsZero" match="/">
	    <xsl:param name="paramNSGNumber"/>	    
            <xsl:message>(New)Input Data (Zero) template called <xsl:copy-of select="$paramNSGNumber"/></xsl:message>   
        </xsl:template> 
</xsl:stylesheet>

Open in new window


What I am trying to do is to implement "with-param" and "call-template" in the same file scope and for some reason it is not working. This xsl file is used in a tool called "IBM DataPower" and its compiler currently supports xslt 1.0 Any responses are welcome. Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The main issue is that your stylesheet will not load as a valid stylesheet

You are using a variable or parameter that is not declared in the scope where it is used
<xsl:if test="$inputData='0'">
Unless your stylesheet is imported in a bigger one, you need a declaration of the parameter

try starting your stylesheet like this
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="inputData" select="0"></xsl:param>

Open in new window


mccarl is right about the two attributes on the named template
In XSLT1 a template should have only a @name OR an @match, not both

BUT that should not stop the processor from doing the right thing
you will get an "ambiguous rule match warning", but the processing should happen correctly
(as per specification. I have tested that on 6 different XSLT processors and they all do what you would have expected, no access to IBM DataPower however)
Avatar of Jammerules
Jammerules

ASKER

That's it. Removing "Match" did the trick. Thanks McCarl
Thanks Geert. Yes, you are correct about the warning too. When I compiled that original code in XMLSpy, much to my frustration, it did not warn me. And when I ran it against the DataPower compiler, I did get a warning but nothing around the area of "Match" keyword not needed.
Your welcome! :)
What strikes me is.... did your code work without declaring the $inputData?

well, your warning tells you that you have too many templates with match="/"
Can I piggyback another question? Say, if I want to call a template and pass a value as a parameter based on a condition. Sorry if it is confusing but here is the code I have:

<xsl:choose>
	<xsl:when test="$rowsReturnedExtensions > '0'">
		<xsl:call-template name="RetryAttemptValueNonZero">
			<xsl:with-param name="paramCSOPhoneNumber" select="$ExtensionsCSOPhoneNumber"/>
		</xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
		<xsl:variable name="resultAccounts" select="dp:sql-execute('OrclDBConnxn',$queryAccountsTable)"/>
		<xsl:variable name="AccountsCSOPhoneNumber" select="string-length($resultAccounts/sql/row/column[3]/value)"/>
		<xsl:call-template name="RetryAttemptValueNonZero">
			<xsl:with-param name="paramCSOPhoneNumber" select="$AccountsCSOPhoneNumber"/>
		</xsl:call-template>
	</xsl:otherwise>
</xsl:choose>  

Open in new window



The template being called:
<xsl:template name="RetryAttemptValueNonZero"> 
    	<xsl:param name="paramCSOPhoneNumber"/>  
        <xsl:message> <xsl:value-of select="$paramCSOPhoneNumber"/></xsl:message> 
</xsl:template>

Open in new window


So, when I execute this code, I am getting errors in DataPower (again, not in XMLSpy. I am hating it when it happens). I had to remove/comment out the place where I am passing the parameter (paramCSOPhoneNumber). I am sure I am doing something wrong here but I am hoping there is another way to accomplish this.
Yes Geert. I deleted it accidentally when I edit-pasted the piece of code on here. Originally, I am retrieving the $inputData
What is the error that you are getting? I assume that the variable "ExtensionsCSOPhoneNumber" is defined?
yessir, it is. I am attaching the complete code file.
Logic.xsl
What is the error that you are getting?
I was hoping that you could answer THAT question too?

But looking at the real XSL now, I would be pretty sure that the problem lies in the datapower (dp) extensions. Because I don't have that product, I can't run your XSL as is. However, by removing all the references to dp: functions and replacing what's left with just some test data, the whole thing runs fine with no errors at all. (Using both MSXML3 and Saxon 9 HE)

So without an error message, it is very hard to help further.