Recursion in XML Schema?

I need to create an XML schema that validates the tree structure of an XML document. I do not know exactly what state or depth level of the tree.

XML example:

<?xml version="1.0" encoding="utf-8"?> <node> <attribute/> <node> <attribute/> <node/> </node> </node> 

What is the best way to test it? Recursion?

+46
xsd
Sep 29 '08 at 14:41
source share
2 answers

if you need a recursive type declaration, here is an example that might help:

 <xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="node" type="nodeType"></xs:element> <xs:complexType name="nodeType"> <xs:sequence minOccurs="0" maxOccurs="unbounded"> <xs:element name="node" type="nodeType"></xs:element> </xs:sequence> </xs:complexType> </xs:schema> 

As you can see, this defines a recursive schema with only one node named "node", which can be as deep as possible.

+66
Sep 29 '08 at 14:45
source share

XSD really allows recursion of elements. Here is a model for you.

 <xsd:element name="section"> <xsd:complexType> <xsd:sequence> <xsd:element ref="title"/> <xsd:element ref="para" maxOccurs="unbounded"/> <xsd:element ref="section" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> 

As you can see, the section element contains a child element that has a type section.

+40
Sep 29 '08 at 14:44
source share



All Articles