Solved

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

Posted on 2014-03-18
11
555 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 59
SUM 2 INTEGER ARRAYS INTO 1 10 58
Path to Python 9 42
What language/protocol is the Angular Chat? 2 0
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

705 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

15 Experts available now in Live!

Get 1:1 Help Now