How to test in Schematron: element value begins with

Questions about XML that are not covered by the other forums should go here.
Iwicka
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

How to test in Schematron: element value begins with

Post by Iwicka »

Hi,

I'm a beginner both in schematron and in xpath. I need to write an assertion testing that an element value always begins with a certain string if the attribute has a certain value:

Code: Select all

<communicationChannel communicationChannelCode="WEBSITE">http://www.oxygenxml.com</communicationChannel>
If the attribute is WEBSITE, the communicationChannel value must begin with http:// . Unfortunately, I cannot crack it, would be grateful for any help

Code: Select all

<sch:rule context="communicationChannel/@communicationChannelCode = 'WEBSITE'>
<sch:assert test="communicationChannel [text() ... />
</sch:rule>
That's where I'm stuck. Help please.

Ewa
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: How to test in Schematron: element value begins with

Post by Jamil »

Hello. I took a quick look at what you are trying to do. I got it to work with the following changes. You will need to change this if you are using namespaces.

First, here is my sample XML document:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="schematron_xml.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<test>
<communicationChannel communicationChannelCode="WEBSITE">http://www.oxygenxml.com</communicationChannel>
<communicationChannel communicationChannelCode="WEBSITE">www.oxygenxml.com</communicationChannel>
</test>
Here is my schema:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron"
queryBinding="xslt2">
<pattern>
<rule context="/test/communicationChannel">
<assert test="./@communicationChannelCode='WEBSITE' and starts-with(.,'http://')">communicationChannel element must start with 'http://'</assert>
</rule>
</pattern>
</schema>
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: How to test in Schematron: element value begins with

Post by Jamil »

There was a bug in my schema above that I am fixing with this change:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron"
queryBinding="xslt2">
<pattern>
<rule context="/test/communicationChannel[@communicationChannelCode='WEBSITE']">
<assert test="starts-with(.,'http://')">communicationChannel element must start with 'http://'</assert>
</rule>
</pattern>
</schema>
I found this issue after running a few more negative test scenarios. The issue was trying to combine two tests in a single assert tripped up the validation. Moving the attribute test to the rule's context resolved this.
Post Reply