[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xml-dev
Subject:    [xml-dev] XSD problem - variable content in foreign namespace
From:       Hans-Juergen Rennau <hrennau () yahoo ! de>
Date:       2022-05-31 16:36:26
Message-ID: 1053586414.8653506.1654014986928 () mail ! yahoo ! com
[Download RAW message or body]

Hello,
I stumbled upon an XSD modelling problem, which is probably insolvable, but perhaps I \
overlook something and you might tell me. The larger topic may be summarized \
"Modelling elements in foreign namespaces".   Here, "foreign namespace" means a \
namespace different from the namespace of the root element. The root element \
namespace I call the local namespace. Accordingly, I speak of local and foreign XSD. \
Now the *problem*: How to define multiple content models for an element name in a \
foreign namespace? In other words: how to write the XSD so that <x:foo> elements (x \
denoting a foreign namespace) can have different content dependent on where it \
appears. In the following example, <x:foo> has different content dependent on if it's \
a child of <b> or <c>: <a xmlns:x="http://example.com/x">      <b>            <y/>    \
<x:foo>                  <x:ONE/>                  <x:TWO/>            </x:foo>       \
</b>      <c>            <y/>            <x:foo>                  <x:ONE/>            \
<x:TEN/>            </x:foo>                 </c></a> How to write XSD reflecting \
this? In general, I see two approaches how to model elements in a foreign \
namespace:(1) Element reference - e.g. <xs:element ref="x:foo"/>(2) Imported type - \
e.g. <xs:element name="foo" type="x:footype"/> Limitation of (1): the same reference \
QName means the same thing everywhere, so the document cannot contain <x:foo> \
elements with different content models, dependent on location. Limitation of (2): as \
the type of a complex element describes its *children*, the namespace appears in the \
*children* of the element with the foreign type, not the element itself. So in the \
example, I would have to use a type from namespace x in order to describe <x:foo>'s \
parent - <b> and <c>. This is possible but twisted, as it is more natural to have the \
type definitions for <b> and <c> in the local XSD. Even worse, it gets really \
cumbersome when the element in the foreign namespace has siblings in the local \
namespace, as is the case in the example - note the <y> sibling of <x:foo>. So one \
has to make a wild construction - writing into the foreign XSD types describing local \
elements <b> and <c>, which will have to reference local element declarations or \
element types (example: reference <y>), in order to achieve the mixing of namespaces. \
Mental ping pong.   So would you agree that supporting variable content models for \
foreign element QNames is tricky and not straightforward at all - or do I overlook a \
simpler possibility? Thank you for ideas or comments!Hans-Jürgen
PS: Here come the XSDs which I wrote to model the document:
a.xsd:<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"              \
xmlns:x="http://example.com/x"  xmlns="http://example.com/a"                        \
targetNamespace="http://example.com/a"              elementFormDefault="qualified"> \
<xs:import namespace="http://example.com/x" schemaLocation="x.xsd"/>  <xs:element \
name="a"> <xs:complexType> <xs:sequence> <xs:element name="b" type="x:bType"/> \
<xs:element name="c" type="x:cType"/> </xs:sequence> </xs:complexType> </xs:element>  \
<xs:element name="y" type="xs:string"/></xs:schema> x.xsd:<xs:schema \
xmlns:xs="http://www.w3.org/2001/XMLSchema"        xmlns:a="http://example.com/a" \
xmlns="http://example.com/x"             targetNamespace="http://example.com/x"       \
elementFormDefault="qualified">      <xs:import namespace="http://example.com/a" \
schemaLocation="a.xsd"/>             <xs:complexType name="bType">            \
<xs:sequence>                  <xs:element ref="a:y"/>                  <xs:element \
name="foo">                        <xs:complexType>                              \
<xs:sequence>                                    <xs:element name="ONE"/>             \
<xs:element name="TWO"/>                              </xs:sequence>                  \
</xs:complexType>                                                 </xs:element>       \
</xs:sequence>                   </xs:complexType>             <xs:complexType \
name="cType">            <xs:sequence>                  <xs:element ref="a:y"/>       \
<xs:element name="foo">                        <xs:complexType>                       \
<xs:sequence>                                    <xs:element name="ONE"/>             \
<xs:element name="TEN"/>                              </xs:sequence>                  \
</xs:complexType>                  </xs:element>            </xs:sequence>            \
</xs:complexType></xs:schema>


[Attachment #3 (text/html)]

<html><head></head><body><div class="yahoo-style-wrap" style="font-family:Helvetica \
Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" \
data-setdir="false">Hello,</div><div dir="ltr" data-setdir="false"><br></div><div \
dir="ltr" data-setdir="false">I stumbled upon an XSD modelling problem, which is \
probably insolvable, but perhaps I overlook something and you might tell \
me.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" \
data-setdir="false">The larger topic may be summarized "Modelling elements in foreign \
namespaces".&nbsp; Here, "foreign namespace" means a namespace different from the \
namespace of the root element. The root element namespace I call the local namespace. \
Accordingly, I speak of local and foreign XSD.</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Now the *problem*: \
How to define multiple content models for an element name in a foreign \
namespace?</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" \
data-setdir="false">In other words: how to write the XSD so that &lt;x:foo&gt; \
elements (x denoting a foreign namespace) can have different content dependent on \
where it appears. In the following example, &lt;x:foo&gt; has different content \
dependent on if it's a child of &lt;b&gt; or &lt;c&gt;:</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div><div \
dir="ltr" data-setdir="false">&lt;a \
xmlns:x="http://example.com/x"&gt;</div><div>&nbsp; &nbsp; &lt;b&gt;</div><div \
dir="ltr" data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &lt;y/&gt;</div><div \
dir="ltr" data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &lt;x:foo&gt;</div><div \
dir="ltr" data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;x:ONE/&gt;</div><div dir="ltr" data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &lt;x:TWO/&gt;</div><div dir="ltr" data-setdir="false">&nbsp; &nbsp; \
&nbsp; &nbsp; &lt;/x:foo&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp;&nbsp;</div><div>&nbsp; &nbsp; &lt;/b&gt;</div><div dir="ltr" \
data-setdir="false">&nbsp; &nbsp; &lt;c&gt;</div><div dir="ltr" \
data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &lt;y/&gt;</div><div dir="ltr" \
data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &lt;x:foo&gt;</div><div dir="ltr" \
data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;x:ONE/&gt;</div><div dir="ltr" data-setdir="false">&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &lt;x:TEN/&gt;</div><div dir="ltr" data-setdir="false">&nbsp; &nbsp; \
&nbsp; &nbsp; &lt;/x:foo&gt;&nbsp; &nbsp; &nbsp; &nbsp;</div><div dir="ltr" \
data-setdir="false">&nbsp; &nbsp; &lt;/c&gt;</div><div>&lt;/a&gt;</div></div><div \
dir="ltr" data-setdir="false"><br style="color: rgb(0, 0, 0); font-family: \
&quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"></div></div>How to write \
XSD reflecting this?</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" \
data-setdir="false">In general, I see two approaches how to model elements in a \
foreign namespace:</div><div dir="ltr" data-setdir="false">(1) Element reference - \
e.g. &lt;xs:element ref="x:foo"/&gt;</div><div dir="ltr" data-setdir="false">(2) \
Imported type - e.g. &lt;xs:element name="foo" type="x:footype"/&gt;</div><div \
dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Limitation \
of (1): the same reference QName means the same thing everywhere, so the document \
cannot contain &lt;x:foo&gt; elements with different content models, dependent on \
location.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" \
data-setdir="false">Limitation of (2): as the type of a complex element describes its \
*children*, the namespace appears in the *children* of the element with the foreign \
type, not the element itself. So in the example, I would have to use a type from \
namespace x in order to describe &lt;x:foo&gt;'s parent - &lt;b&gt; and &lt;c&gt;. \
This is possible but twisted, as it is more natural to have the type definitions for \
&lt;b&gt; and &lt;c&gt; in the local XSD. Even worse, it gets really cumbersome when \
the element in the foreign namespace has siblings in the local namespace, as is the \
case in the example - note the &lt;y&gt; sibling of &lt;x:foo&gt;. So one has to make \
a wild construction - writing into the foreign XSD types describing local elements \
&lt;b&gt; and &lt;c&gt;, which will have to reference local element declarations or \
element types (example: reference &lt;y&gt;), in order to achieve the mixing of \
namespaces. Mental ping pong.&nbsp;</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">So would you agree \
that supporting variable content models for foreign element QNames is tricky and not \
straightforward at all - or do I overlook a simpler possibility?</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Thank you for ideas \
or comments!</div><div dir="ltr" data-setdir="false">Hans-Jürgen</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">PS: Here come the \
XSDs which I wrote to model the document:</div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">a.xsd:</div><div \
dir="ltr" data-setdir="false"><div><div>&lt;xs:schema \
xmlns:xs="http://www.w3.org/2001/XMLSchema"&nbsp;</div><div><span style="white-space: \
pre-wrap;">	</span>&nbsp; &nbsp; &nbsp; \
&nbsp;xmlns:x="http://example.com/x"&nbsp;xmlns="http://example.com/a"<span \
style="white-space: pre-wrap;">	</span>&nbsp; &nbsp; &nbsp; &nbsp;</div><div><span \
style="white-space: pre-wrap;">	</span>&nbsp; &nbsp; &nbsp; \
&nbsp;targetNamespace="http://example.com/a"&nbsp;</div><div><span \
style="white-space: pre-wrap;">	</span>&nbsp; &nbsp; &nbsp; \
&nbsp;elementFormDefault="qualified"&gt;</div><div><span style="white-space: \
pre-wrap;">	</span>&lt;xs:import namespace="http://example.com/x" \
schemaLocation="x.xsd"/&gt;</div><div><span style="white-space: \
pre-wrap;">	</span></div><div><span style="white-space: \
pre-wrap;">	</span>&lt;xs:element name="a"&gt;</div><div><span style="white-space: \
pre-wrap;">		</span>&lt;xs:complexType&gt;</div><div><span style="white-space: \
pre-wrap;">			</span>&lt;xs:sequence&gt;</div><div><span style="white-space: \
pre-wrap;">				</span>&lt;xs:element name="b" type="x:bType"/&gt;</div><div><span \
style="white-space: pre-wrap;">				</span>&lt;xs:element name="c" \
type="x:cType"/&gt;</div><div><span style="white-space: \
pre-wrap;">			</span>&lt;/xs:sequence&gt;</div><div><span style="white-space: \
pre-wrap;">		</span>&lt;/xs:complexType&gt;</div><div><span style="white-space: \
pre-wrap;">	</span>&lt;/xs:element&gt;<span style="white-space: \
pre-wrap;">	</span></div><div><span style="white-space: \
pre-wrap;">	</span>&lt;xs:element name="y" \
type="xs:string"/&gt;</div><div>&lt;/xs:schema&gt;</div><div><br></div></div>x.xsd:</div><div \
dir="ltr" data-setdir="false"><div><div>&lt;xs:schema \
xmlns:xs="http://www.w3.org/2001/XMLSchema"&nbsp;</div><div>&nbsp; &nbsp; \
xmlns:a="http://example.com/a" xmlns="http://example.com/x"&nbsp; \
&nbsp;&nbsp;</div><div>&nbsp; &nbsp; \
targetNamespace="http://example.com/x"&nbsp;</div><div>&nbsp; &nbsp; \
elementFormDefault="qualified"&gt;</div><div>&nbsp; &nbsp; &lt;xs:import \
namespace="http://example.com/a" schemaLocation="a.xsd"/&gt;</div><div>&nbsp; \
&nbsp;&nbsp;</div><div>&nbsp; &nbsp; &lt;xs:complexType \
name="bType"&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:element ref="a:y"/&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:element name="foo"&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &lt;xs:complexType&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:element \
name="ONE"/&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:element name="TWO"/&gt;</div><div>&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;/xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &lt;/xs:complexType&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;/xs:element&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/xs:sequence&gt;&nbsp; \
&nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; \
&lt;/xs:complexType&gt;</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; \
&lt;xs:complexType name="cType"&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:element ref="a:y"/&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;xs:element name="foo"&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &lt;xs:complexType&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:element \
name="ONE"/&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &lt;xs:element name="TEN"/&gt;</div><div>&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;/xs:sequence&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &lt;/xs:complexType&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&lt;/xs:element&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/xs:sequence&gt;&nbsp; \
&nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; \
&lt;/xs:complexType&gt;</div><div>&lt;/xs:schema&gt;</div><div><br></div></div><br></div><div \
dir="ltr" data-setdir="false"><br></div></div></body></html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic