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 August 30th, 2013, 09:42 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default using a function to populate an element

Hi All,

i am have some XML that is lay out as follows;
Code:
<drivers>
     <driver2licence>A</driver2licence>
     <driver3licence>I</driver3licence>
     <driver4licence>E</driver4licence>
<drivers>
Each value needs to be mapped to a different value, at different points in the output xml, i am thinking a function that i can call from that will look up and check the correct mapping each time, i have NEVER used a function before, so i am a little lost;
the function (in my understanding) i can call this function and tell it where its "root is" and it will look there (.) and check the relevant mappings and return a value.
Code:
<xsl:function name="licence" as="xs:string">
	<xsl:choose>
		<xsl:when test=". = 'A'">F</xsl:when>
		<xsl:when test=". = 'I'">N</xsl:when>
		<xsl:when test=". = 'E'">M</xsl:when>
		<xsl:otherwise>Z</xsl:otherwise>																												
	</xsl:choose>
</xsl:function>
then to call this function driver 2,3,4 would be as follows
Code:
<Driver2>
     <licence>	
          <xsl:apply-templates select="$simple/driver2licence[licence(.)]"/>
     </licence>
</Driver2>
<Driver3>
     <licence>	
          <xsl:apply-templates select="$simple/driver3licence[licence(.)]"/>
     </licence>
</Driver3>
The main problem is between /driver2 and <driver3> there will be another 15-20 nodes that access different data.
Can you please modify, or throw and away and re-write how i would go about this? I hope the code provided helps explain what i want.

The reason i want this is currently i have 20 fields under each Parent Driver node, and it is just chunks and chunks of repeated code, which when i need to change a value, i have to change 5-25 times for each driver and each scenario. understanding how to use a function to use 1 set of mappings for various fields will allow me drastically reduce the transform i am writting.
 
Old August 30th, 2013, 11:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you want the output of the function, then you want something like this:

Code:
<xsl:value-of select="license($simple/driver2license/text())"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 30th, 2013, 11:19 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for the reply,

just figured it out, was missing a few things... Like a namespace

got it all working and so far the existing 3400line transform is down to 1600 and counting :D

Code:
<xsl:function name="fns:getLic" as="xs:string">
		<xsl:param name="root" as="xs:string"/>
		<xsl:choose>
			<xsl:when test="$root = 'F'"><xsl:value-of select ="'F'"/></xsl:when>
			<xsl:when test="$root = 'P'"><xsl:value-of select ="'P'"/></xsl:when>
			<xsl:when test="$root = 'A'"><xsl:value-of select ="'F'"/></xsl:when>
			<xsl:when test="$root = 'I'"><xsl:value-of select ="'N'"/></xsl:when>
			<xsl:when test="$root = 'E'"><xsl:value-of select ="'E'"/></xsl:when>
			<xsl:otherwise><xsl:value-of select ="'Z'"/></xsl:otherwise>
		</xsl:choose>
	</xsl:function>
then using the following to call
Code:
<xsl:value-of select="fns:getLic($simple/proposerlicence)"/>
<xsl:value-of select="fns:getLic($simple/driver2licence)"/>
 
Old September 13th, 2013, 07:04 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default hard coded param calls

Hi,

I have a 340 line piece of code that is repeated 5 times, i wish to have this in a function, and call it 5 times, some values are hard-coded in the xml for each call however, so if i can pass these in as parameters. this would allow me to reduce this to 330ish lines instead of 1600lines.

Currently testing functions calling functions and as a start test i am trying to pass in a number, this number isn't used but will be...
Below are the basic functions so far;
Code:
<xsl:function name="fns:getLic" as="xs:string">
		<xsl:param name="toot" as="xs:string"/>
		<xsl:param name="root" as="xs:string"/>
		<xsl:value-of select="fns:getLic2($root)"/>
	</xsl:function>

	<xsl:function name="fns:getLic2" as="xs:string">
		<xsl:param name="root" as="xs:string"/>
		<xsl:choose>
			<xsl:when test="$root = 'F'">F</xsl:when>
			<xsl:when test="$root = 'P'">P</xsl:when>
			<xsl:when test="$root = 'A'">F</xsl:when>
			<xsl:when test="$root = 'I'">N</xsl:when>
			<xsl:when test="$root = 'E'">E</xsl:when>
			<xsl:otherwise>Z</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
and the call for this (i call it 5 times)
Code:
<xsl:value-of select="fns:getLic('1', $simple/proposer)"/>
<xsl:value-of select="fns:getLic('2', $driver/driver2licence)"/>
<xsl:value-of select="fns:getLic('3', $driver/driver3licence)"/>
<xsl:value-of select="fns:getLic('4', $driver/driver4licence)"/>
<xsl:value-of select="fns:getLic('5', $driver/driver5licence)"/>
can you advise how i would pass this 1/2/3/4/5 in? as it seems to cause problems with compiling the stylesheet. when i send as is above, even though the value is not used.

to sum up;
 
Old September 13th, 2013, 07:11 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you get an error then please tell us the exact error message.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 13th, 2013, 09:49 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi,

I have got around ym previous problem however, i am gettign the below error;
Code:
Error on line 274 of file:/C:/eclipse_workspace/quoteshub/xslt/qa%20-%20Functions/NU/PC/DD/NB/V1/QuoteRequest.xslt:
  XPTY0019: Required item type of first operand of '/' is node(); supplied value has item
  type xs:string
This is due to i am calling a function and sending $claims, into it, this refers to a section of code like below
Code:
<claims>
     <claimcat>a</claimcat>
     <claimCode>1</claimCode>
     <claimcost>500</claimcost>
</claims>
In the function i do things like
Code:
<xsl:value-of select="$claim/acc5pi" />
Can you advise what i need to declare this param as for the function to be able to pick up the "parent" and all "child" information so the above is possible?
 
Old September 13th, 2013, 09:55 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

item()*
would be the thing i was looking for a believe! however i have come across a problem and in my research, i think i may of hit a dead end.

Some of these tags may be blank or not exist, this would result in a param not being populated.

Line#: 564; Column#: -1
net.sf.saxon.trans.DynamicError: An empty sequence is not allowed as the 5th argument of fns:getDriverInfo()

Jonathan Spickernell
Software Technician
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.

Last edited by Aygith; September 13th, 2013 at 10:06 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Function to exclude Parent Element Srinibondugula XSLT 1 May 12th, 2013 05:00 AM
Identify element based on the element data/content ROCXY XSLT 7 September 8th, 2011 02:48 AM
Chapter 9: Element[user control] is not a known element Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 20th, 2009 07:31 AM
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
populate drop down list with a function code3hree Javascript 1 May 28th, 2005 11:44 AM





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