Question

I'm trying to write a Schema for a xml similar to this

<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
    <book>title1</book>
    <poster>some text</poster>
    <poster>some text</poster>
    <book>title1</book>
    <poster>some text</poster>
</store>

I'd like to know if there is a complexType in Schema that allows the "book" and "poster" tags to appear a minumum of 0 times and an unlimited maximum number of times. I need them to be able to appear in a casual order like: book, poster, book poster, poster.

I though I could simply not put a sequence, and write a schema like this

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="store" type="TStore"/>

    <xs:complexType name="TStore">
        <xs:element name="book" type="xs:string" maxOccurs="unbounded"/>
        <xs:element name="poster" type="xs:string" maxOccurs="unbounded"/>
    </xs:complexType>
</xs:schema>

But this just isn't valid schema. The "xs:all" would be good if the book and poster tags appeared in a casual order, but only once. How can I fix it? EDIT: a xs:choice with maxOccurs="unbounded" does the trick nicely.

I also don't understand why a simple repetition like this should require a sequence. I consulted some tutorial, but I can't find a reference for this simple case.

<?xml version="1.0" encoding="ISO-8859-1"?>
<tags>
    <tag>text1</tag>
    <tag>text2</tag>
    <tag>text3</tag>
</tags>

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="tags" type="complexType">
        <xs:sequence>
            <xs:element name="tag" type="xs:string" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:element>
</xs:schema>
Was it helpful?

Solution

I also don't understand why a simple repetition like this should require a sequence

Is this your actual question? The simple answer is that the specification requires it.

The more detailed answer is that the validator wants an unambiguous specification of what it should look for. The <xs:sequence> unambiguously tells the validator that its contents represent an expected sequence of elements in the instance document. Just like <xs:choice> tells the validator that it should see only one of the contained elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top