473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XSD for requiring elements with certain attributes

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?

Jul 20 '05 #1
2 1316


mo********@gmail.com 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/
Jul 20 '05 #2
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

<mo********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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?

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leif K-Brooks | last post by:
I'm writing a site with mod_python which will have, among other things, forums. I want to allow users to use some HTML (<em>, <strong>, <p>, etc.) on the forums, but I don't want to allow bad...
0
by: Parker Thompson | last post by:
I'd like to define an element (complexType) that *must* contain certain elements one or more times, but may additionally contain any other elements. Further, I need all elements to be albe to...
2
by: Novice | last post by:
Hi all, I have to decide on an XML structure going forward. The structure is going to house a large amount of data. In the past I've always just used the philosophy of "when in doubt use...
1
by: lltaylor2000 | last post by:
Hello, I am creating an XML Document using XmlDocument. I can create my top node no problem However when I add child nodes they seem to inherit the xmlns="" attribute (see xml below). How...
1
by: Foxpointe | last post by:
Given some arbitrary XHTML, I'd like to obtain a 'simplified' XHTML result which strips out a large subset of standard elements and attributes - but not all. The main things I would like to...
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
11
by: shankwheat | last post by:
I have a function which passes text from txtdebt to debtsbox which works fine. However, I want to add code which examines the value of debtsbox and if any of the values the user entered contain the...
6
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def>...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.