Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml schema - single and multi element occurrence

Tags:

xsd

I have an XML schema with my "video" element and "youtube" element inside:

<xs:element name="video">
  <xs:complexType>            
    <xs:sequence>     
      <xs:element ref="youtube" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>    
    <xs:attribute name="file" type="xs:string"/>
  </xs:complexType>  
</xs:element>

and I want to add to available video types another element "param":

<xs:element name="param">
   <xs:complexType>
     <xs:attribute name="name" type="xs:string" use="required"/>
     <xs:attribute name="value" type="xs:string" use="required"/>
   </xs:complexType>
</xs:element>    

I would have "param" element inside next to "youtube".

Restrictions:

  • youtube can occur only one time in my xml
  • param - can occur many times in my xml

like this:

<video>
<youtube file = "aaa"/>
<param name="a1" value"b1"/>
<param name="a2" value"b2"/>
</video>

How to maintain this restrictions in this schema?

If I do something like this:

<xs:sequence>     
      <xs:element ref="youtube" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="param" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>    

if the sequence is specified like this

<xs:sequence>     
      <xs:element ref="youtube" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="param" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>    

it determines that param has to be after youtube - but I don't want to specify the sequence

like image 889
Przemysław Michalski Avatar asked Oct 11 '22 12:10

Przemysław Michalski


1 Answers

Here is XSD schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="video">
        <xs:complexType>
            <xs:choice minOccurs="1" maxOccurs="2">
                <xs:element ref="youtube"/>
                <xs:element ref="param" minOccurs="1" maxOccurs="unbounded"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:element name="youtube">
        <xs:complexType>
            <xs:attribute name="file" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="param">
        <xs:complexType>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="value" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Example (well-formed and valid) XML:

<?xml version="1.0" encoding="UTF-8"?>
<video>
    <youtube file="aaa"/>
    <param name="a1" value="b1"/>
    <param name="a2" value="b2"/>
</video>

There is one issue with above solution. xs:choice allows two youtube elements. There is xs:all element, which solve that, but unfortunately it's impossible to get param's maxOccurs more than one with it (xs:all limitation).

In other words there is no 100% solution using XML Schema 1.0. If above is not sufficient you can use another schema language such as RELAX NG or Schematron.

edit:

In XML Schema 1.1 (superset of XML Schema 1.0) you can write video element as:

<xs:element name="video">
    <xs:complexType>
        <xs:all>
            <xs:element ref="youtube"/>
            <xs:element ref="param" minOccurs="0" maxOccurs="unbounded"/>
        </xs:all>
    </xs:complexType>
</xs:element>
like image 107
13 revs, 2 users 99% Avatar answered Jan 01 '23 11:01

13 revs, 2 users 99%