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 December 7th, 2009, 04:34 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default Functions with multiple optional parameters

I have a function that has two parameters.. I'd like to be able to call it with either one or both parameters.. is it possible to do this in XSLT 2.0 without having to resort to empty arguments in the function call? For example:

Code:
<xsl:function name="lbs:myfn" as="xs:string">
		<xsl:param name="param1" as="xs:string"/>
		<xsl:param name="param2" as="xs:string"/>
 <!-- some logic here -->
</xsl:function>
I'd like to be able to call the function with one or both of param1 and param2.. I see that there's an optional attribute for param but not sure how the function should be invoked if I do specify both the above as optional and want to call it with just one param.. thanks!
 
Old December 7th, 2009, 04:38 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I believe you can simply specify the parameter with a default value:

<xsl:param name="param1" as="xs:string" select="''"/>

Edit: Sorry, its a function you're talking about. Can't remember, let me look it up.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 7th, 2009, 04:42 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Sorry, optional parameters for functions are not allowed:

http://www.w3.org/TR/xslt20/#stylesheet-functions
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 7th, 2009, 04:42 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

So when I invoke the function, I still need to pass 2 arguments as follows (assuming I want to invoke with one real argument for param2):

lbs:myfn('','blah')

If so, this would be quite cumbersome when the number of parameters is large, wouldn't it?
 
Old December 7th, 2009, 04:43 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can write another function with the same name that only has one parameter, which calls your 2-param version supplying the default value for the second parameter. But you can't make both arguments optional this way, it would be ambiguous.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 7th, 2009, 04:47 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Right, I implemented that for another case but that would only work if exactly one parameter was optional.. so it appears for the multiple optional parameters case, I need to write multiple functions... :(
 
Old December 7th, 2009, 05:31 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>If so, this would be quite cumbersome when the number of parameters is large, wouldn't it?

If the number of arguments is more than 3 or 4 then there's probably something wrong with your design. Consider passing a structured XML object as a single argument.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Alternative to optional parameters in C# asptwodev BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 November 12th, 2007 03:47 AM
Web Service with multiple functions deer_reema ASP.NET 1.0 and 1.1 Basics 2 May 3rd, 2006 01:52 PM
Are Optional Parameters supported in Access 2000? howardb1 Access VBA 2 April 26th, 2006 10:01 AM
optional parameters in SP yuqlin BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 7 July 13th, 2004 03:28 PM
Optional Stored Proc Parameters? VBAHole22 SQL Server 2000 3 August 13th, 2003 11:46 AM





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