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

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

December 7th, 2009, 04:38 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|

December 7th, 2009, 04:42 PM
|
|
Authorized User
|
|
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
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?
|
|

December 7th, 2009, 04:43 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

December 7th, 2009, 04:47 PM
|
|
Authorized User
|
|
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
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... :(
|
|

December 7th, 2009, 05:31 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|
 |