Re: [xsl] strong typed variable with restriction ?

Subject: Re: [xsl] strong typed variable with restriction ?
From: Matthieu Ricaud-Dussarget <matthieu.ricaud@xxxxxxxxx>
Date: Wed, 02 Feb 2011 14:06:23 +0100
I tried to override the original function with this one :
<xsl:function name="igs:get-css-rule" as="element()*">
<xsl:param name="e" as="element()"/>
<xsl:param name="csspath" as="xs:string"/>
<xsl:param name="mode" as="xs:string"/><!--(override|not-override|both)-->
<xsl:param name="css" as="xs:string"/><!--(csstidy|css)-->
<xsl:sequence select="igs:get-css-rule($e,$csspath,$mode cast as igs:override_mode, $css cast as igs:css_mode)"/>
</xsl:function>


But as the number of params is the same with the original function, I get a duplicate function error.
Would have been great to be able to discern functions from typed params and not only numbers of param, but so it is...


[offtopic : on the other hand SaxonHE is not schema aware, this is a good argument to get saxonEE , I'll ask my boss !]

Thanks for this last advice Michel, using <xsl:message terminate="yes">...</> would be an acceptable solution for me at this point.

Thanks all for your help,

Matthieu.
Le 02/02/2011 13:43, Michel Hendriksen a icrit :
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




--
Matthieu Ricaud
IGS-CP
Service Livre numirique

Current Thread