Schematron test not working
This should cover W3C XML Schema, Relax NG and DTD related problems.
Schematron test not working
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>
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>
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: Schematron test not working
Post by sorin_ristache »
Hello,
Regards,
Sorin
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: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>
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
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: Schematron test not working
Post 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:
Regards,
Sorin
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
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
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
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: Schematron test not working
Post 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
Regards,
Sorin
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service