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. Advertisements

  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. Advertisements

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. Replies:
    2
    Views:
    1,261
    Henry S. Thompson
    Mar 6, 2006
  2. anonymous
    Replies:
    1
    Views:
    6,350
  3. =?Utf-8?B?TWFyaw==?=
    Replies:
    4
    Views:
    808
    =?Utf-8?B?TWFyaw==?=
    Dec 14, 2006
  4. perspolis
    Replies:
    1
    Views:
    3,025
    Mark Rae
    Jan 23, 2007
  5. Joe Kovac
    Replies:
    2
    Views:
    1,337
    Joe Kovac
    May 2, 2007
  6. John
    Replies:
    16
    Views:
    1,066
  7. CdnRebel
    Replies:
    4
    Views:
    2,346
    CdnRebel via DotNetMonster.com
    Jul 4, 2007
  8. Dan
    Replies:
    2
    Views:
    608
Loading...