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

How to test in Schematron: element value begins with

Fri Nov 29, 2013 6:01 pm

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: 77

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

Sun Dec 01, 2013 11:27 pm

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: 77

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

Sun Dec 01, 2013 11:41 pm

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.

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest