Solved

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

Posted on 2014-03-18
11
564 Views
Last Modified: 2014-03-19
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.
0
Comment
Question by:Jammerules
  • 5
  • 4
  • 2
11 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39938942
Try removing the match="/" attribute from the second (named) template, like so...
<?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">
	    <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

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39939044
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)
0
 

Author Closing Comment

by:Jammerules
ID: 39940985
That's it. Removing "Match" did the trick. Thanks McCarl
0
 

Author Comment

by:Jammerules
ID: 39940991
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.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39940992
Your welcome! :)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39941025
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="/"
0
 

Author Comment

by:Jammerules
ID: 39941034
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.
0
 

Author Comment

by:Jammerules
ID: 39941042
Yes Geert. I deleted it accidentally when I edit-pasted the piece of code on here. Originally, I am retrieving the $inputData
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39941053
What is the error that you are getting? I assume that the variable "ExtensionsCSOPhoneNumber" is defined?
0
 

Author Comment

by:Jammerules
ID: 39941068
yessir, it is. I am attaching the complete code file.
Logic.xsl
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39941227
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now