Page 1 of 1

XSL problem with numbers bigger than 100000

Posted: Thu May 24, 2012 12:43 pm
by bohansen
Hi,

I have a list of ranges to validate for overlap. E.g.:

Code: Select all


<intervals>
<range start="10000" end="15000"/>
<range start="20000" end="25000"/>
<range start="80000" end="85000"/>
<range start="90000" end="95000"/>
<!-- (1) <range start="90000" end="95000"/> -->
<!-- (2) <range start="100000" end="105000"/> -->
</intervals>
Put together from a date range function I have the following XSL function to validate for overlap:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:fct="http://custom"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:template match="intervals">
<xsl:choose>
<xsl:when test="fct:check_overlap((),range)">No overlap</xsl:when>
<xsl:otherwise>Overlap!!</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:function name="fct:check_overlap" as="xs:boolean">
<xsl:param name="current_end" as="xs:integer?"/>
<xsl:param name="ranges" as="node()*"/>
<xsl:choose>
<xsl:when test="not(exists($current_end))">
<xsl:variable name="orderedRanges" as="node()*">
<xsl:for-each select="$ranges">
<xsl:sort select="@start"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="fct:check_overlap(
xs:integer($orderedRanges[position()=1]/@end),
$orderedRanges[position()>1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="
if (not(exists($ranges))) then true() else
if ($current_end > $ranges[position()=1]/@start) then false() else
fct:check_overlap(
xs:integer($ranges[position()=1]/@end),
$ranges[position()>1])
"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

</xsl:stylesheet>
The functions seems to work fine if I run it on the data shown above. If I uncomment (1) it correctly tells that the ranges are overlapping. However whenever I have a range with number above 100000 the function return overlap even though that's not the case.
I'm wondering if I'm doing something wrong in the code or there's a bug in OxygenXML editor? (version 13.2)

Thanks in advance,
Bo

Re: XSL problem with numbers bigger than 100000

Posted: Thu May 24, 2012 3:36 pm
by Costin
Hello,

The cause of the incorrect result thrown by the "check_overlap" function is a sorting error that has crept in your code.

More specifically, when you sort the ranges (node sequences) by the "@start" attribute, the sorting is alphabetical, instead of numerical. So, in alphabetical terms, 100000 is lower than 95000 for example.
To achieve the desired results, the value of the "@start" attribute has to be cast to integer first.
For that, you should modiffy the line

Code: Select all

<xsl:sort select="@start"/>
to

Code: Select all

<xsl:sort select="xs:integer(@start)"/>
Regards,
Costin

Re: XSL problem with numbers bigger than 100000

Posted: Fri May 25, 2012 3:19 pm
by bohansen
Thanks for a quick reply!
That solved my problem (and found a similar conversion problem in another function ;-)).

Is it possible to import custom XSL functions to a schematron file?

/Bo

Re: XSL problem with numbers bigger than 100000

Posted: Fri May 25, 2012 5:43 pm
by Costin
Hi again,

Yes, it is possible to use custom XSL functions in a schematron file.
For that, you should first check the "Allow foreign elements" option from oXygen Options > Preferences > XML > XML Parser.

To make a better idea, here is an example of using such a function:

The XML file:

Code: Select all

<?xml-model href="Test.sch" type="application/xml"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<test>
</test>
The schematron file, with an example function:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" xmlns:x="http://www.example.com/x"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<ns uri="http://www.example.com/x" prefix="x"/>

<xsl:function name="x:f" >
<xsl:text>DATA FROM FUNCTION</xsl:text>
</xsl:function>
<pattern id="p1">
<rule context="test">
<assert test="false()">
Test function <value-of select="x:f()"/>
</assert>
</rule>
</pattern>
</schema>
Hope this helps.

Regards,
Costin

Re: XSL problem with numbers bigger than 100000

Posted: Fri May 25, 2012 5:47 pm
by bohansen
Thanks, I already got that far - what puzzles me is if I can put the xsl-function in a separate file? I'm already using the functions for some normal XSL stylesheets.

Best regards,
Bo

Re: XSL problem with numbers bigger than 100000

Posted: Mon May 28, 2012 3:57 pm
by adrian
Hi,

You can put XSL functions (xsl:function) in a separate file and include it in schematron. Make sure that the included file contains only XSL functions. Specifically, make sure that this included file does not contain XSL templates or it might create a mess.

Regards,
Adrian

Re: XSL problem with numbers bigger than 100000

Posted: Tue May 29, 2012 12:37 pm
by bohansen
Thanks, I had some trouble getting the include correct, but the following seems to work and get the namespace correct also:

Code: Select all


  <xsl:include href="custom_fct.xsl"/>
<ns prefix="fct" uri="http://custom"/>
Best regards,
Bo