Schematron position() bug

Having trouble installing Oxygen? Got a bug to report? Post it all here.
dsewell
Posts: 125
Joined: Mon Jun 09, 2003 6:02 pm
Location: Charlottesville, Virginia USA

Schematron position() bug

Post by dsewell »

It seems that the Schematron validation included with oXygen has a bug involving XPath tests with position().

Consider this sample XML file:

Code: Select all

<test>
<table>
<row n="1">
<cell>one</cell>
<cell>two</cell>
</row>
<row n="2">
<cell>un</cell>
<cell>deux</cell>
</row>
<row n="3">
<cell>uno</cell>
<cell>dos</cell>
</row>
</table>
</test>
I want to enforce a rule that every row must have an @n attribute containing the row number. This Schematron rule should do it:

Code: Select all

<sch:rule context="row">
<sch:assert test="@n and ( position() = @n )">Each row should be
numbered sequentially using the @n attribute</sch:assert>
</sch:rule>
But it does not work under oXygen 5.1 (OS X version). All the rows fail.

After some experiment, I discovered that the rows all return true() for the tests if I change

Code: Select all

position() = @n
to

Code: Select all

position() = @n* 2 
So it seems that the true position is being multiplied by 2 somewhere in the underlying Schematron code.

I tested this data and my original Schematron rule using Jing (version 20030619) and it performed as expected.

Then I downloaded the Skeleton 1.5 files from http://xml.ascc.net/schematron/1.5/ and tried running the code using a command-line XSLT processor. This produced the bug also. But I can't figure out where the problem is in Skeleton.

Since oXygen already comes with Jing, maybe Jing could be used for the Schematron support? See:

http://www.thaiopensource.com/relaxng/jing-other.html
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi David,

The result given by the Schematron validation in oXygen takes into account the text nodes that you have between the row elements. If you elliminate those nodes you will get the document validated.

You can use Jing to validate with a Schematon schema if you place that in a NRL schema like below:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<rules xmlns="http://www.thaiopensource.com/validate/nrl">
<anyNamespace>
<validate schema="test.sch"/>
</anyNamespace>
</rules>
and then validate with that NRL schema.

A more roboust approach will be if you change your Schematron rule as follows:

Code: Select all


<sch:rule context="row">
<sch:assert test="@n and ( count(preceding-sibling::row) = @n - 1 )">Each row should be
numbered sequentially using the @n attribute</sch:assert>
</sch:rule>
Best Regards,
George
dsewell
Posts: 125
Joined: Mon Jun 09, 2003 6:02 pm
Location: Charlottesville, Virginia USA

Thanks, but confused

Post by dsewell »

George, thanks for the revised Schematron rule. I am still confused about whether the Skeleton 1.5 validation is correct or buggy. If I write an XSLT script to report position() for each row in the sample XML file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//row">
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
it produces the output "1 2 3" using any of the XSLT parsers in oXygen. So I would expect a Schematron rule with context "row" and test "position()" to give the same result.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi David,

Skeleton does not work like that, it uses templates to match the row elements. So it does something like:

Code: Select all


<xsl:template match="row">
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
</xsl:template>
and a default template will be also applied on the table element:

Code: Select all


<xsl:template match="table">
<xsl:apply-templates select="node()"/>
</xsl:template>
As you can see this will get the text nodes when you use position.

Best Regards,
George
Last edited by george on Thu Jan 27, 2005 10:58 am, edited 1 time in total.
dsewell
Posts: 125
Joined: Mon Jun 09, 2003 6:02 pm
Location: Charlottesville, Virginia USA

Post by dsewell »

Thanks for the explanation!
Post Reply