[XMLSCHEMA-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Repeating elements with fixed attribute values

From: Andrew Welch <andrew.j.welch@gmail.com>
Date: Fri, 4 Jan 2008 15:40:57 +0000
Message-ID: <74a894af0801040740n261b0c2bh52653d3c0aac492@mail.gmail.com>
To: "King, Jeffrey (Mission Systems)" <Jeff.King@ngc.com>
Cc: xmlschema-dev@w3.org
Re: Repeating elements with fixed attribute values

On 04/01/2008, King, Jeffrey (Mission Systems) <Jeff.King@ngc.com> wrote:
> I have an xml document similar to this:
>
> <root>
>   <word name="one">
>     <field name="field1" number="1">any string here</field>
>     <field name="field2" number="2">any string here</field>
>     <field name="field3" number="3">any string here</field>
>     …
>   </word>
>   <word name="two">
>     <field name="field4" number="4">any string here</field>
>     <field name="field5" number="5">any string here</field>
>     <field name="field6" number="6">any string here</field>
>     …
>   </word>
> </root>
>
> The attributes have fixed values.  I am struggling to create a schema for
> this.

If you mean you want to ensure that @number="1" if @name="field1" and
so on, then that's a "co-occurrence constraint", which isn't possible
to define in XML Schema.

It will be possible in XML Schema 1.1 (check out Saxon's schema
processor), but in the mean time you'll need to do the check in
another language, like Schematron or RelaxNG.

If you're feeling brave you could do the check using xchecker - the
schema would look something like this (note the <xck:check> element):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xck="http://xchecker.sf.net/"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xs:annotation>
        <xs:appinfo>
            <xck:check>//field[@number eq translate(@name, 'field',
'')]</xck:check>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="root" type="root"/>
    <xs:element name="word" type="word"/>
    <xs:element name="field" type="field"/>

    <xs:complexType name="root">
        <xs:sequence>
            <xs:element ref="word" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="word">
        <xs:sequence>
            <xs:element ref="field" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="name" use="required" type="non-empty-string"/>
    </xs:complexType>

    <xs:complexType name="field">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" use="required"
type="non-empty-string"/>
                <xs:attribute name="number" use="required" type="xs:integer"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:simpleType name="non-empty-string">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>   	

</xs:schema>


More here: http://xchecker.sourceforge.net/


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/
Received on Friday, 4 January 2008 15:41:09 GMT

Subscribe to the Stylus Scoop newsletter for helpful XML tips and tutorials.
Email
First Name
Last Name
Company

Download Stylus Studio 6 XML Enterprise Edition

Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.