Page 1 of 1

A schema for interlacing elements from different schemas

Posted: Tue Feb 24, 2009 10:53 pm
by luchm
Hi All,

Suppose we have a schema root-a-b.xsd that validates documents like:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="root-a-b.xsd">
<a>
<b/>
</a>
<a>
<b/>
<b/>
</a>
</root>
Now I'd like to define a schema interlace.xsd that should validate documents such as the following:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<in:interlace xmlns:in="myURI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="myURI interlace.xsd">
<root>
<!-- Loops on both levels -->
<in:loop>
<a>
<in:loop>
<b/>
</in:loop>
</a>
</in:loop>

<!-- Internal loop only -->
<a>
<in:loop>
<b/>
</in:loop>
</a>

<!-- External loop only -->
<in:loop>
<a>
<b/>
</a>
</in:loop>
</root>
</in:interlace>
Basically, I'd like interlace.xsd to validate documents that:
  • Have in:interlace as their document node
  • Do not have any empty in:loop element
  • Are valid root-a-b.xsd documents once all in: elements are skipped
I could probably write an XSLT script to generate interlace.xsd from root-a-b.xsd, but I was wondering if there would be a way to do that in such a way that root-a-b.xsd is only referenced, and so that a modification in this schema (say, now <b> elements can contain <c> elements) would be automatically reflected, without having to regenerate interlace.xsd. Again, I'm not set on xsd schemas -- if NG or another schema model would work better for that, I'm sold.

Any pointers?

Thanks!

Re: A schema for interlacing elements from different schemas

Posted: Mon Jun 01, 2009 5:33 pm
by george
Hi,

I know this comes late but here it is!
You can use an NVDL script to perform basically 2 validations:
- one validates the in:interlace and all the in:loop together with the in:loop content against the interlace.xsd schema
- the other validates the elements from no namespace against the root-a-b.xsd schema:

Code: Select all


<rules xmlns="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
<namespace ns="http://myURI">
<validate schema="interlace.xsd">
<mode>
<namespace ns="">
<validate schema="root-a-b.xsd">
<mode>
<namespace ns=""><attach/></namespace>
<anyNamespace><unwrap/></anyNamespace>
</mode>
</validate>
<unwrap>
<mode>
<anyNamespace><attach/></anyNamespace>
</mode>
</unwrap>
</namespace>
<namespace ns="http://myURI">
<attach/>
</namespace>
</mode>
</validate>
</namespace>
</rules>
rppt-a-b.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b">
<xs:complexType/>
</xs:element>
</xs:schema>
interlace.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://myURI" xmlns:in="http://myURI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="interlace">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element ref="in:loop"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="loop">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="in:loop"/>
<xs:any namespace="##local" processContents="lax"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
sample xml

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<?oxygen NVDLSchema="test.nvdl"?>
<in:interlace xmlns:in="http://myURI">
<root>
<!-- Loops on both levels -->
<in:loop>
<a>
<in:loop>
<b/>
</in:loop>
</a>
</in:loop>

<!-- Internal loop only -->
<a>
<in:loop>

<b/>
</in:loop>
</a>

<!-- External loop only -->
<in:loop>
<a>
<b/>
</a>
</in:loop>
</root>
</in:interlace>
Best Regards,
George