Page 1 of 1

Schematron test not working

Posted: Wed Oct 01, 2008 7:02 pm
by TechHead
I have a xsd schema with embedded schematron rules as below. It does does not work. Appreciate any ideas. The XML snippet is given below.

Thanks
TechHead

<!--- Schematron Rules -->

<!-- QandA Type -->
<xs:complexType name="qAndAType">
<xs:sequence>
<xs:element name="Question" type="questionType" minOccurs="1">
<xs:annotation>
<xs:appinfo>
<sch:pattern id="answerTests" name="AnswerValueType">
<sch:rule context="sr:AnswerValueType[text()='Count']">
<sch:assert diagnostics="d1" test="count(../sr:QandA/Answer/CountValue) = 1">
If AnswerValueType = Count, there should be a CountValue in Answer
</sch:assert>
</sch:rule>
<sch:rule context="sr:AnswerValueType[text()='Bool']">
<sch:assert diagnostics="d1" test="count(../sr:Answer/BoolValue) = 1">
If AnswerValueType = Bool, there should be a BoolValue in Answer
</sch:assert>
</sch:rule>
</sch:pattern>
<sch:diagnostics>
<sch:diagnostic id="d1"> Value of count = <sch:value-of select="test"/> </sch:diagnostic>
</sch:diagnostics>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="Answer" type="answerType" minOccurs="1" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>


XML is as below showing only relevant tags.

<QandA>
<Question>
<QuestionNumber>A1</Question>
<AnswerValueType>Bool</AnwwerValueType>
</Question>
<Answer>
<BoolValue>true</BoolValue>
</Answer>
</QandA>

Re: Schematron test not working

Posted: Thu Oct 02, 2008 10:06 am
by sorin_ristache
Hello,
TechHead wrote: <xs:appinfo>
<sch:pattern id="answerTests" name="AnswerValueType">
<sch:rule context="sr:AnswerValueType[text()='Count']">
<sch:assert diagnostics="d1" test="count(../sr:QandA/Answer/CountValue) = 1">If AnswerValueType = Count, there should be a CountValue in Answer</sch:assert>
</sch:rule>
<sch:rule context="sr:AnswerValueType[text()='Bool']">
<sch:assert diagnostics="d1" test="count(../sr:Answer/BoolValue) = 1">If AnswerValueType = Bool, there should be a BoolValue in Answer</sch:assert>
</sch:rule>
</sch:pattern>
<sch:diagnostics>
<sch:diagnostic id="d1"> Value of count = <sch:value-of select="test"/> </sch:diagnostic>
</sch:diagnostics>
</xs:appinfo>
You did not include in the fragment the declaration of the sr namespace prefix used in sr:AnswerValueType. You should declare the sr prefix like the following:

Code: Select all

    <xs:appinfo>
<sch:ns prefix="sr" uri="the_namesapce_URI_for_the_sr_prefix"/>
<sch:pattern id="answerTests" name="AnswerValueType">
...

Regards,
Sorin

Re: Schematron test not working

Posted: Mon Oct 06, 2008 7:49 pm
by TechHead
I used the prefix.
It still does not work. Any other ideas.

Thanks
-Shashi

Re: Schematron test not working

Posted: Tue Oct 07, 2008 12:00 pm
by sorin_ristache
Hello,

You have to add the sr prefix to all the elements from that namespace used in the Schematron rules, so you should use test="count(../sr:QandA/sr:Answer/sr:CountValue) = 1" instead of test="count(../sr:QandA/Answer/CountValue) = 1", etc. I added below a complete example:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?oxygen SCHSchema="testSchematronForum.xsd"?>
<QandA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="namespace_URI testSchematronForum.xsd"
xmlns="namespace_URI">
<Question>
<QuestionNumber>A1</QuestionNumber>
<AnswerValueType>Bool</AnswerValueType>
<!-- <AnswerValueType>Count</AnswerValueType>-->
</Question>
<Answer>
<BoolValue>true</BoolValue>
</Answer>
<Answer>
<CountValue>5</CountValue>
</Answer>
</QandA>

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://www.ascc.net/xml/schematron"
targetNamespace="namespace_URI" elementFormDefault="qualified"
xmlns="namespace_URI">

<xs:complexType name="questionType">
<xs:sequence>
<xs:element name="QuestionNumber" type="xs:string"/>
<xs:element name="AnswerValueType" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="answerType">
<xs:choice>
<xs:element name="BoolValue" type="xs:string"/>
<xs:element name="CountValue" type="xs:string"/>
</xs:choice>
</xs:complexType>

<!-- QandA Type -->
<xs:complexType name="qAndAType">
<xs:sequence>
<xs:annotation>
<xs:appinfo>
<sch:ns prefix="sr" uri="namespace_URI"/>
<sch:pattern id="answerTests" name="AnswerValueType">
<sch:rule context="sr:Question/sr:AnswerValueType[1][. = 'Count']">
<sch:assert test="count(../../sr:Answer/sr:CountValue) = 1">
If AnswerValueType = Count, there should be a CountValue in Answer:
<sch:value-of select="count(../../sr:Answer/sr:CountValue)"/>
</sch:assert>
</sch:rule>
<sch:rule context="sr:Question/sr:AnswerValueType[1][. = 'Bool']">
<sch:assert test="count(../../sr:Answer/sr:BoolValue) = 1">
If AnswerValueType = Bool, there should be a BoolValue in Answer:
<sch:value-of select="count(../../sr:Answer/sr:BoolValue)"/>
</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
<xs:element name="Question" type="questionType" minOccurs="1"/>
<xs:element name="Answer" type="answerType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:element name="QandA" type="qAndAType"/>
</xs:schema>

Regards,
Sorin

Re: Schematron test not working

Posted: Tue Oct 07, 2008 10:04 pm
by TechHead
Thanks Sorin.

It is working now.
Does Oxygen save the XSLT that validates the rules.If so how can I access it?
The reason I ask is because we want our customers to validate the xml before sending it to us.

Appreciate your help and suggestions.

Regards
TechHead

Re: Schematron test not working

Posted: Wed Oct 08, 2008 3:37 pm
by sorin_ristache
No, Oxygen does not save the generated XSLT stylesheet on disk. It takes into account the Schematron options that are set in Preferences -> XML -> XML Parser and generates an internal XSLT stylesheet which is applied on the XML document. You can generate an XSLT stylesheet that applies the Schematron constraints on your XML documents with the Schematron skeleton stylesheet (the stylesheet is here).


Regards,
Sorin