•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the XML, XSLT and XPATH section within the Software Development category of DaniWeb, a massive community of 456,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,485 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our XML, XSLT and XPATH advertiser: Programming Forums
Views: 1744 | Replies: 5
![]() |
•
•
Join Date: Aug 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
I want to model a situation where you can have multiple children of an element, and the children can be different types and in any order. A simple example of what I mean is at [1]. I am trying to make a schema to define this document, my attempted schema at [2].
If I understand the format correctly I need to make the parent element (building) a complex type, and this must have an order indicator to contain other elements. None of the 3 order indicators (All, Choice, Sequence) fit my model.
How do I do this? It seems I am missing something fundamental, or I am trying to model data that cannot be fit to xml.
Thank you for any help.
If I understand the format correctly I need to make the parent element (building) a complex type, and this must have an order indicator to contain other elements. None of the 3 order indicators (All, Choice, Sequence) fit my model.
How do I do this? It seems I am missing something fundamental, or I am trying to model data that cannot be fit to xml.
Thank you for any help.
[1]
<building name="house1">
<window state="open"/>
<door state="open"/>
<door state="closed"/>
<window state="closed"/>
</building>
[2]
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="building">
<xs:annotation>
<xs:documentation>a building</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence> <!-- This tag is the problem -->
<xs:element name="window" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="state" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="door" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="state" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
replace <sequence> with <choice>, and use the attributes 'minOccurs="0" maxOccurs="unbounded"'. Take out the maxOccurs on each of the potential children in the choice. Something like this:
What does this mean? it means that the author of a document may choose only one of the potential children, but do so as many times as they like; i.e. they can pick children in any order and any number of times. It's a nasty workaround to, what I feel is a nasty shortcoming in the W3C schema language. It's a nasty workaround, because you lose any level of evaluation of the actual provided sequence of children; all you can say is that there's some number of them.
Please, please don't assume that if you can't describe something, or can't well define it with W3C schema that the data "shouldn't" be modelled in XML!! there are other schema languages, RelaxNG/Schematron for eg. W3C schema are near-awful for anything where structure is more important than datatyping.
XML Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="building"> <xs:annotation> <xs:documentation>a building</xs:documentation> </xs:annotation> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="window" > <xs:complexType> <xs:attribute name="state" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:element name="door" > <xs:complexType> <xs:attribute name="state" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema>
Please, please don't assume that if you can't describe something, or can't well define it with W3C schema that the data "shouldn't" be modelled in XML!! there are other schema languages, RelaxNG/Schematron for eg. W3C schema are near-awful for anything where structure is more important than datatyping.
Plato forgot the nullahedron..
•
•
Join Date: Aug 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
replace <sequence> with <choice>, and use the attributes 'minOccurs="0" maxOccurs="unbounded"'. Take out the maxOccurs on each of the potential children in the choice. Something like this:
...
Thanks, that does exactly what I want. It does not seems to do what w3schools says it does:
from http://www.w3schools.com/schema/sche...indicators.asp
The <choice> indicator specifies that either one child element or another can occur:
Is there a more accurate description of what these tags really mean?
Thank you for the help.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
It does mean that only one of the elements within the choice can be chosen - but putting maxOccurs = unbounded means that you can make that choice an unbounded number of times at that point... It's similar to how one would express the same constraint in DTD language:
i.e.
<!ELEMENT house ( door | window | roof ) * >
That means, house may contain ( ANY NUMBER OF [ * ] ( door OR window OR roof ) ).. so, first element is a door, ok that meets ( door OR window OR roof ), second is a door, ok that meets ( door OR window OR roof ), third is a roof.. and so on. Since your allowed an unbounded number of choices, you can pick any of the elements allowed by the choice, any number of times, in any order.
i.e.
<!ELEMENT house ( door | window | roof ) * >
That means, house may contain ( ANY NUMBER OF [ * ] ( door OR window OR roof ) ).. so, first element is a door, ok that meets ( door OR window OR roof ), second is a door, ok that meets ( door OR window OR roof ), third is a roof.. and so on. Since your allowed an unbounded number of choices, you can pick any of the elements allowed by the choice, any number of times, in any order.
Last edited by MattEvans : Oct 17th, 2007 at 10:54 am.
Plato forgot the nullahedron..
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
Hm, the best way to learn how to use w3c schema is to actually look at the working schema for some XML languages, that and consulting the actual specification for schema http://www.w3.org/XML/Schema#dev ...although, w3cschools isn't that helpful since it only details each element's functionality singularly, not really as they interact with each other.
Plato forgot the nullahedron..
![]() |
•
•
•
•
•
•
•
•
DaniWeb XML, XSLT and XPATH Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- Little Simple array question :) (C)
- Newbie Question - How to generate a link (PHP)
- Redirecting - Newbie Question (PHP)
- Program alone doesn't help. (C++)
- Newbie Question - Hiding php extension (PHP)
- Stupid Newbie Question (C)
- AGP Aperture Size Question (Windows NT / 2000 / XP / 2003)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: Xpath navigation in xml file
- Next Thread: vbscript variable within a CDATA node


Linear Mode