validating element occurance based on attribute values

Discussion in 'XML' started by Martin Honnen, Jul 31, 2004.

  1. UndoMiel wrote:

    > 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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 31, 2004
    #1
    1. Advertising

  2. Martin Honnen

    UndoMiel Guest

    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!
     
    UndoMiel, Jul 31, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R3JpZ3M=?=

    Strange Occurance on Live but not on Test Server

    =?Utf-8?B?R3JpZ3M=?=, Oct 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    278
    =?Utf-8?B?R3JpZ3M=?=
    Oct 13, 2004
  2. news.hku.hk
    Replies:
    7
    Views:
    7,683
    Michiel Salters
    Apr 26, 2004
  3. Adam Teasdale Hartshorne
    Replies:
    1
    Views:
    325
    Thomas Tutone
    Aug 11, 2005
  4. John
    Replies:
    16
    Views:
    890
  5. magix
    Replies:
    3
    Views:
    315
    user923005
    May 21, 2008
Loading...

Share This Page