Subject: Re: [xsl] strong typed variable with restriction ? From: Michel Hendriksen <michel.hendriksen@xxxxxxxxx> Date: Wed, 2 Feb 2011 13:43:10 +0100 |
Or add a message in an xsl:otherwise... <xsl:message terminate="yes|no"> <!-- Content:template --> </xsl:message> On Wed, Feb 2, 2011 at 12:21 PM, Michael Kay <mike@xxxxxxxxxxxx> wrote: > There seem to be two separate questions here. >> >> I have a xsl:function which : >> - must return a element() >> - has a string param "foobar". >> >> I typed $foobar as xs:string but I'd like to restrict the possible values >> to "foo" or "bar". >> I know it's possible to define such a constrain in a xsd schema, but is >> there a way to do that in xpath2 ? > > You can declare a local type in your stylesheet: > > <xsl:stylesheet xmlns:fb="urn:local:foobar" ....> > > <xsl:import-schema> > <xs:schema targetNamespace="urn:local:foobar"> > <xs:simpleType name="foobarType"> > <xs:restriction base="xs:string"> > <xs:enumeration value="foo"/> > <xs:enumeration value="bar"/> > </xs:restriction> > </xs:simpleType> > </xs:schema> > </xsl:import-schema> > > <xsl:function name="igs:get-css-rule" as="element()"> > <xsl:param name="foobar" as="fb:foobarType"/> > .... > > Note that when calling the function, you can't pass a string directly: you > will have to cast ot to fb:foobarType first. > > >> >> >> This typing is important because the returned element() is selected from a >> xsl:choose on $foobar value (with no otherwise) : >> >> My code looks like : >> <xsl:function name="igs:get-css-rule" as="element()"> >> <xsl:param name="foobar" as="xs:string"/> <!--(foo|bar)--> >> <xsl:choose> >> <xsl:when test="$foobar='foo'"> >> <xsl:sequence select="igs:get-my-foo-item()"/> >> </xsl:when> >> <xsl:when test="$css='bar'"> >> <xsl:sequence select="igs:get-my-bar-item()"/> >> </xsl:when> >> </xsl:choose> >> </xsl:function> >> >> And I get such a parsing error on my xslt : >> XTTE0570: Conditional expression: If none of the conditions is satisfied, >> an empty >> sequence will be returned, but this is not allowed [...] > > Typing the parameter unfortunately won't solve this problem. The processor > isn't clever enough to work out that the implicit "otherwise" branch will > never be executed, so it will still report a type error to the effect that > the implicit "otherwise" branch returns a value (namely the empty sequence) > which doesn't conform to the required return type of the function. (There > has been some suggestion that Saxon's type checking here is too pessimistic; > the type error should be raised dynamically if this branch is taken, not > statically). The easiest way around this is probably to change the last > xsl:when condition to an xsl:otherwise. > > Michael Kay > Saxonica
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] strong typed variable wit, Matthieu Ricaud-Duss | Thread | Re: [xsl] strong typed variable wit, Matthieu Ricaud-Duss |
Re: [xsl] strong typed variable wit, Matthieu Ricaud-Duss | Date | Re: [xsl] strong typed variable wit, Matthieu Ricaud-Duss |
Month |