Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old April 10th, 2013, 03:54 PM
Authorized User
Points: 340, Level: 6
Points: 340, Level: 6 Points: 340, Level: 6 Points: 340, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default Restrict custom function parameters

Let's say you define a custom function. When declaring a parameter, I could use the "as" flag to define the valid data type on the input parameter of that function. I normally would rely on the XML Schema datatypes (in the xs: namespace) to restrict the input parameter type. But what if I wanted to have a pattern restriction or custom datatype on that parameter?

For example, I want a function that takes a string with the enumeration of "X" and "Y" only. If any other string is passed as an input to that function, the function would 'fail'.

I know you can define a custom type in XML Schema and import that custom data type into your XSLT for use. But can it be done with regular expressions or perhaps some other mechanism?

Code:
<xsl:function name="fns:doSoemthing" as="xs:string">
	<xsl:param name="p1" as="xs:string" />
	<!-- Only 'X' and 'Y' are valid for p1 -->

	<xsl:value-of select="$p1" />
</xsl:function>
  #2 (permalink)  
Old April 10th, 2013, 04:17 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sure you can do this:

Code:
<xsl:import-schema>
  <xs:schema>
    <xs:simpleType name="X-or-Y">
      <xs:restriction base="xs:string">
        <xs:pattern value="[X|Y]"/>
      </
    </
  </
</
<xsl:function name="f:do" as="X-or-Y">
  <xsl:param name="$in" as="X-or-Y"/>
  <xsl:sequence select="X-or-Y(translate($in, 'XY', 'YX'))"/>
</xsl:function>
The main reason people don't do this, however, is that it's inconvenient, because you can't call f:do('X'), you have to call f:do(X-or-Y('X')). In fact you probably need a namespace for the type as well, I would have to check the rules.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
  #3 (permalink)  
Old April 10th, 2013, 04:20 PM
Authorized User
Points: 340, Level: 6
Points: 340, Level: 6 Points: 340, Level: 6 Points: 340, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Thanks for the reply. That is the method that I expected to have to use. As far as the namespace definitions, I can check the rules as well, but that's a minor detail. I can see how it would be somewhat cumbersome to invoke (not to mention ugly to look at). I can also circle around this by checking the inputs before invoking the functions and throwing exceptions prior to the actual function all. That's much easier done in XSLT, than dealing with custom schema imports.

Thanks again!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters to a function atcs2152 Ajax 4 December 22nd, 2005 09:12 AM
passing empty parameters to a js function crmpicco Javascript How-To 0 September 20th, 2005 10:16 AM
Passing parameters to a In([MyParameter]) function Rocket Classic ASP Databases 0 March 17th, 2005 10:21 AM
C# passing parameters to custom control class booksnore2 General .NET 1 October 5th, 2004 11:44 AM
Custom SQL parameters sphair BOOK: Professional Crystal Reports for VS.NET 1 April 6th, 2004 11:52 PM





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