XSD for requiring elements with certain attributes

Discussion in 'XML' started by monsterpot@gmail.com, Mar 23, 2005.

  1. Guest

    Hello,
    I have a document like this:

    <parameters>
    <param ptype="init">somedata</param>
    <param ptype="process">moredata</param>
    <param ptype="kill">alsodata</param>
    </parameters>

    <param> has the following definition in my XSD:

    <xs:complexType name="param">
    <xs:simpleContent>
    <xs:extension base="xs:string">
    <xs:attribute name="ptype" type="xs:string" use="required" />
    </xs:extension
    </xs:simpleContent>
    </xs:complexType>

    The question I have is: I want to require <parameters> elements to
    contain exactly 3 <param> elements, and furthermore, to require one
    element with the attribute ptype="init", one element with
    ptype="process", and another with ptype="kill".

    So, for example I want to invalidate this document:

    <parameters>
    <param ptype="init">somedata</param>
    <param ptype="kill">alsodata</param>
    </parameters>

    because it is missing an element with ptype="process".

    I am somewhat new to XSD. Could someone help me?
    , Mar 23, 2005
    #1
    1. Advertising

  2. wrote:


    > I have a document like this:
    >
    > <parameters>
    > <param ptype="init">somedata</param>
    > <param ptype="process">moredata</param>
    > <param ptype="kill">alsodata</param>
    > </parameters>
    >
    > <param> has the following definition in my XSD:
    >
    > <xs:complexType name="param">
    > <xs:simpleContent>
    > <xs:extension base="xs:string">
    > <xs:attribute name="ptype" type="xs:string" use="required" />
    > </xs:extension
    > </xs:simpleContent>
    > </xs:complexType>
    >
    > The question I have is: I want to require <parameters> elements to
    > contain exactly 3 <param> elements,


    That would be
    <xsl:element name="parameters">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="param" />
    <xs:element ref="param" />
    <xs:element ref="param" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    > and furthermore, to require one
    > element with the attribute ptype="init", one element with
    > ptype="process", and another with ptype="kill".


    I don't think you can model such a constraint with a W3C XML schema.
    There you would need to use different kinds of child elements e.g.
    <parameters>
    <init>somedata</init>
    <process>moredata</process>
    <kill>alsodata</kill>
    </parameters>
    and model that as a sequence.

    Or you could do as suggested earlier above and model your additional
    constraints in a schema language more suitable, schematron, see
    <http://xml.ascc.net/resource/schematron/schematron.html>
    there you could specify a constraint on the attributes.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 24, 2005
    #2
    1. Advertising

  3. The following schema will do what you need. It defines a sequence with
    three occurrences of param element and then uses a uniqueness constraint to
    enforce unique attributes.

    <?xml version="1.0" encoding="utf-8" ?>

    <xs:schema targetNamespace="http://myschemas/foo"

    elementFormDefault="qualified"

    xmlns:foo="http://myschemas/foo"

    xmlns="http://myschemas/foo"

    xmlns:xs="http://www.w3.org/2001/XMLSchema">



    <xs:complexType name="paramType">

    <xs:simpleContent>

    <xs:extension base="xs:string">

    <xs:attribute name="ptype" type="paramAttrType" use="required" />

    </xs:extension>

    </xs:simpleContent>

    </xs:complexType>



    <xs:simpleType name="paramAttrType">

    <xs:restriction base="xs:string">

    <xs:enumeration value="init"/>

    <xs:enumeration value="process"/>

    <xs:enumeration value="kill"/>

    </xs:restriction>

    </xs:simpleType>



    <xs:complexType name="paramsType">

    <xs:sequence>

    <xs:element name="param" minOccurs="3" maxOccurs="3"
    type="paramType"/>

    </xs:sequence>

    </xs:complexType>



    <xs:element name="params" type="paramsType">

    <xs:unique name="uniqueParams">

    <xs:selector xpath="foo:param"/>

    <xs:field xpath="@ptype"/>

    </xs:unique>

    </xs:element>

    </xs:schema>


    --
    Stan Kitsis
    Program Manager, XML Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm

    <> wrote in message
    news:...
    > Hello,
    > I have a document like this:
    >
    > <parameters>
    > <param ptype="init">somedata</param>
    > <param ptype="process">moredata</param>
    > <param ptype="kill">alsodata</param>
    > </parameters>
    >
    > <param> has the following definition in my XSD:
    >
    > <xs:complexType name="param">
    > <xs:simpleContent>
    > <xs:extension base="xs:string">
    > <xs:attribute name="ptype" type="xs:string" use="required" />
    > </xs:extension
    > </xs:simpleContent>
    > </xs:complexType>
    >
    > The question I have is: I want to require <parameters> elements to
    > contain exactly 3 <param> elements, and furthermore, to require one
    > element with the attribute ptype="init", one element with
    > ptype="process", and another with ptype="kill".
    >
    > So, for example I want to invalidate this document:
    >
    > <parameters>
    > <param ptype="init">somedata</param>
    > <param ptype="kill">alsodata</param>
    > </parameters>
    >
    > because it is missing an element with ptype="process".
    >
    > I am somewhat new to XSD. Could someone help me?
    >
    Stan Kitsis [MSFT], Mar 24, 2005
    #3
    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. Replies:
    0
    Views:
    649
  2. Gooseman
    Replies:
    1
    Views:
    411
    DFN-CIS NetNews Service
    Jan 22, 2004
  3. Parker Thompson
    Replies:
    0
    Views:
    357
    Parker Thompson
    Apr 29, 2005
  4. Robert Zurer
    Replies:
    2
    Views:
    469
    Robert Zurer
    Jul 7, 2005
  5. Joshua Mostafa
    Replies:
    4
    Views:
    2,287
    Joshua Mostafa
    May 11, 2007
Loading...

Share This Page