XSD for requiring elements with certain attributes

M

monsterpot

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?
 
M

Martin Honnen

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

Stan Kitsis [MSFT]

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
 

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,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top