User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2007
Posts: 5
Reputation: hughbedo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hughbedo hughbedo is offline Offline
Newbie Poster

Newbie question, schema, complex types and unordered multiple elemets

  #1  
Oct 16th, 2007
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.
[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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Newbie question, schema, complex types and unordered multiple elemets

  #2  
Oct 16th, 2007
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:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="building">
  4. <xs:annotation>
  5. <xs:documentation>a building</xs:documentation>
  6. </xs:annotation>
  7. <xs:complexType>
  8. <xs:choice minOccurs="0" maxOccurs="unbounded">
  9. <xs:element name="window" >
  10. <xs:complexType>
  11. <xs:attribute name="state" type="xs:string" use="required"/>
  12. </xs:complexType>
  13. </xs:element>
  14. <xs:element name="door" >
  15. <xs:complexType>
  16. <xs:attribute name="state" type="xs:string" use="required"/>
  17. </xs:complexType>
  18. </xs:element>
  19. </xs:choice>
  20. </xs:complexType>
  21. </xs:element>
  22. </xs:schema>
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.
Plato forgot the nullahedron..
Reply With Quote  
Join Date: Aug 2007
Posts: 5
Reputation: hughbedo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hughbedo hughbedo is offline Offline
Newbie Poster

Re: Newbie question, schema, complex types and unordered multiple elemets

  #3  
Oct 17th, 2007
Originally Posted by MattEvans View Post
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.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Newbie question, schema, complex types and unordered multiple elemets

  #4  
Oct 17th, 2007
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.
Last edited by MattEvans : Oct 17th, 2007 at 10:54 am.
Plato forgot the nullahedron..
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Newbie question, schema, complex types and unordered multiple elemets

  #5  
Oct 17th, 2007
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..
Reply With Quote  
Join Date: Aug 2007
Posts: 5
Reputation: hughbedo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hughbedo hughbedo is offline Offline
Newbie Poster

Re: Newbie question, schema, complex types and unordered multiple elemets

  #6  
Oct 17th, 2007
Thank you very much. I understand much better now.

I shall have a look at the specification but it is not a short document.

Thanks again.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb XML, XSLT and XPATH Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the XML, XSLT and XPATH Forum

All times are GMT -4. The time now is 5:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC