dependent values problem in schematron

This should cover W3C XML Schema, Relax NG and DTD related problems.
cal
Posts: 3
Joined: Mon Dec 04, 2006 6:50 pm

dependent values problem in schematron

Post by cal »

Hello,

I am trying to use schematron to validate XML files but I am stuck on a situation that I cannot establish a rule for:
Sample XML:
<parameters>
<parameter>
<name>percentage</name>
<value>89</value>
</parameter>
<parameter>
<name>temperatureF</name>
<value>-274<value>
</parameter>
<parameters>
Rules I want to enforce via schematron:
1. If name = percentage then 0 <= value <= 100
2. If name = temperatureF then -40 <= value <= 40
etc.

The rules are controlled by a dictionarry are there are a lot of possibilities. I can't seem to get around the problem that the condition I want to test depends on the value of an element and the immediate sibling value. Any ideeas would be appreciated. Thank you very much.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

For the parameters element you can use the following XML Schema with embedded Schematron rules. The Schematron rules are selected based on the value of the name element.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="parameters">
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check value interval." xmlns:sch="http://www.ascc.net/xml/schematron">
<sch:rule context="parameter[name='percentage']">
<sch:assert test="value >= 0 and value <= 100">Percentage must be between 0 and 100.</sch:assert>
</sch:rule>
<sch:rule context="parameter[name='temperatureF']">
<sch:assert test="value >= -40 and value <= 40">Temperature must be between -40 and 40.</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:NCName"/>
<xs:element name="value" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Associate the schema with your XML document using the XML Schema tab of the Associate Schema dialog and check the Embedded Schematron rules checkbox.

If there are many values of the name element that you want to check then I think Schematron is not very useful because it takes too much to write manually a Schematron rule for each one. You should check the values with an XSLT stylesheet for example or at the application level.


Regards,
Sorin
Post Reply