validating element occurance based on attribute values

M

Martin Honnen

UndoMiel said:
I have a situation where i would like to validate the occurance of certain
elements, based on the value of an attribute.

What is the "best" way to handle such validations? I am fairly new to XML
and its validation techniques. Schematron would probably do the trick, but i
havent found a straight forward way that solves my problem.

Well schematron should indeed allow that, the summary at
http://www.schematron.com/
explicitly says

Attributes
W3C XML Schemas does not support several idiomatic uses of
attributes: using an attribute to constrain or select an element's
content model, or using using constrainted data values with various
units. It provides no advance on DTDs in this area.

when trying to present capabilities of schematron.

I have never used schematron before but I have just tried to write a
schema as follows

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.ascc.net/xml/schematron">
<pattern name="Test that attribute type=&quot;net&quot; has child
element home">
<rule context="person">
<report test="@type = 'net' and not(home)">necessary child
element home is missing</report>
</rule>
</pattern>
</schema>

which reports an error if a <person> element has an attribute type with
value 'net' but no child element <home>.

An example XML document that should raise an error during validation is

<?xml version="1.0" encoding="UTF-8"?>
<root>
<person type="net">
<name>Kibo</name>
<home>http://www.kibo.com/</home>
</person>
<person type="net">
<name>Xibo</name>
</person>
<person type="normal">
<name>Maho</name>
</person>
</root>

and indeed when I use Jing from
http://www.thaiopensource.com/relaxng/jing.html to perform a validation
it says

PathToFile\test2004073102.xml:7: error: report:
necessary child element home is missing

so it correctly gives an error for the element <person> in line 7 as to
not having a child element <home>.


You can find schematron examples at
http://www.zvon.org/xxl/SchematronTutorial/General/toc.html
 
U

UndoMiel

Hi,

I have a situation where i would like to validate the occurance of certain
elements, based on the value of an attribute.

What is the "best" way to handle such validations? I am fairly new to XML
and its validation techniques. Schematron would probably do the trick, but i
havent found a straight forward way that solves my problem.

Anybody ideas - recommendations?

Thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top