Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 2nd, 2013, 04:43 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default Functions Returning null when called twice?

I have a function as follows

Code:
<xsl:function name="fns:getClassft" as="xs:string">
	<xsl:param name="root" as="xs:string"/>
	<xsl:choose>
		<xsl:when test="$root = 'F'">F</xsl:when>
		<xsl:when test="$root = 'H'">H</xsl:when>
		<xsl:when test="$root = 'O'">I</xsl:when>
		<xsl:when test="$root = 'P'">C</xsl:when>
		<xsl:when test="$root = 'R'">R</xsl:when>
		<xsl:when test="$root = 'S'">S</xsl:when>
		<xsl:when test="$root = 'U'">U</xsl:when>
		<xsl:otherwise>E</xsl:otherwise>																										
	</xsl:choose>
</xsl:function>
This is called using the following;
Code:
<xsl:value-of select="fns:getClassft($simple/proposerclassft)"/>
when in the input xml this field appears twice, the function returns a null and causes the entire transform to fall over.
Code:
<proposerclassft>R</proposerclassft>
<proposerclassft>R</proposerclassft>
If i remove one of the above, it works fine.

Any idea why this is causing the function/transform to stop dead when called?
is there some extra code needed to handle this null and cause it to output the otherwise when a null occurs?
Usually there would not ever be a duplicated field in the XML but i need the transform to be defensively built to not fall over if one were to occur.
Below is a snipper of the transform and the end section (where it fall over) of the xml produced.
Transform;
Code:
&lt;Occupation&gt;
	&lt;Occupation_Code Val='<xsl:value-of select="$simple/proposerocccodeft" />' /&gt;
	&lt;Occupation_EmploymentType Val='<xsl:value-of select="fns:getClassft($simple/proposerclassft)"/>' /&gt;
	&lt;Occupation_EmployersBusiness Val='<xsl:value-of select="$simple/proposerempcodeft" />' /&gt;
	&lt;Occupation_FullTimeEmploymentInd Val='Y' /&gt;
&lt;/Occupation&gt;
Output XML;
Code:
&lt;Occupation&gt
       &lt;Occupation_Code Val='R09' /&gt;
       &lt;Occupation_EmploymentType Val='</xrt:RequestXML></ns:messageIn></ns:ProcessTran></soapenv:Body></soapenv:Envelope>
 
Old September 2nd, 2013, 05:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, there's no such thing as "null" in the XPath data model. What was the actual error message?

Secondly, when you do this

Code:
<xsl:value-of select="fns:getClassft($simple/proposerclassft)"/>
then the function is not called twice, it is called once with a sequence of two element nodes as the argument. That's an error because the function signature says that it expects a single string to be supplied.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Aygith (September 2nd, 2013)
 
Old September 2nd, 2013, 06:00 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Ahh apologies for thta, the test harness i am using just says "null" and stops the xml output at that point, the error is obviously as you have said; that the call for the function is supplying both of the fields in as a argument,

My understanding was that it would call the function for each time it found the field ($simple/proposerclassft). is there a way to set the "call" to only supply the firts case it finds of the field? i.e $simple/proposerclassft[1] , i have used something similar with DateTimes before but not sure if this will work with Functions?
 
Old September 2nd, 2013, 06:04 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to call the function for each selected node use
Code:
$simple/proposerclassft/fns:getClassft(.)
if you want to call it only for the first select node use
Code:
fns:getClassft(($simple/proposerclassft)[1])
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
Aygith (September 2nd, 2013)
 
Old September 2nd, 2013, 06:12 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Martin,

I thought it would be a matter of putting a Position perameter into the function call. that appears to have corrected the issue.

Thanks again to you both!
 
Old September 2nd, 2013, 06:56 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, because you are calling a function which states it takes a string with a sequence instead.

Try simply doing fns:getClassft($simple/proposerclassft[1]) if you are happy to skip anything but the first element in the sequence.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Automatic Variables from Functions jabney BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 November 29th, 2008 09:56 PM
Returning Null Values in a query dstein4d Access VBA 2 March 21st, 2008 03:59 PM
returning null value Vince_421 Access VBA 3 March 6th, 2007 01:54 PM
e.Item.FindControl("DropdownId") Returning Null anup_daware .NET Framework 1.x 1 March 27th, 2006 03:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.