Solved

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

Posted on 2014-03-18
11
595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 36

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 36

Expert Comment

by:mccarl
ID: 39940992
Your welcome! :)
0
 
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 36

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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

729 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