Solved

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

Posted on 2014-03-18
11
574 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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
 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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