Xml Schema - the appearance of one or more elements

I have an XML schema with my "video" element and the "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 another param element to the existing video types:

 <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 a param element inside the youtube.

Limitations:

  • youtube can only happen once 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 keep these restrictions in this scheme?

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 as follows:

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

it determines what the parameter should be after youtube - but I do not want to specify the sequence

+4
source share
1 answer

Here is the 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 problem with the above solution. xs: selection allows you to use two youtube elements . There is an xs: all element that solves this, but unfortunately, it is impossible to get param maxOccurs more than one with it (xs: all restrictions).

In other words, a 100% solution is not used using XML Schema 1.0. If the above is not enough, you can use another schema language such as RELAX NG or Schematron.

edit:

In XML Schema 1.1 (a superset of XML Schema 1.0) you can write a 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> 
+2
source

Source: https://habr.com/ru/post/1343932/


All Articles