Connecting Tech Pros Worldwide Help | Site Map

Xml Schema: Extending base root element with unique/key/keyref element.

Newbie
 
Join Date: Aug 2009
Posts: 5
#1: Aug 4 '09
I know that the selector of these elements has a scope relative to the element being declared, but maybe there is a way to get beyond bounds of this scope or maybe just a way to extend base element?
Here’s a working example:
Expand|Select|Wrap|Line Numbers
  1. <xs:schema id="schema"
  2.     targetNamespace="http://tempuri.org/schema.xsd"
  3.     elementFormDefault="qualified"
  4.     xmlns="http://tempuri.org/schema.xsd"
  5.     xmlns:mstns="http://tempuri.org/schema.xsd"
  6.     xmlns:xs="http://www.w3.org/2001/XMLSchema">
  7.  
  8.     <xs:element name="element" abstract="true" type="TElement"/>
  9.     <xs:element name="local" type="TLocal" substitutionGroup="element"/>
  10.  
  11.     <xs:element name="root" type="TRoot">
  12.         <!--declaring constraint within TRoot (root element of any document)-->
  13.         <xs:unique name="local-name-constraint">
  14.             <xs:selector xpath=".//mstns:local"/>
  15.             <xs:field xpath="@name"/>
  16.         </xs:unique>
  17.     </xs:element>
  18.  
  19.     <xs:complexType name="TElement" abstract="true">
  20.     </xs:complexType>
  21.  
  22.     <xs:complexType name="TRoot">
  23.         <xs:complexContent>
  24.             <xs:extension base="TElement">
  25.                 <xs:sequence>
  26.                     <xs:element ref="element" maxOccurs="unbounded"/>
  27.                 </xs:sequence>
  28.             </xs:extension>
  29.         </xs:complexContent>
  30.     </xs:complexType>
  31.  
  32.     <xs:complexType name="TLocal">
  33.         <xs:complexContent>
  34.             <xs:extension base="TElement">
  35.                 <xs:sequence>
  36.                     <xs:element ref="element" minOccurs="0" maxOccurs="unbounded"/>
  37.                 </xs:sequence>
  38.                 <xs:attribute name="name" use="required" type="xs:string"/>
  39.             </xs:extension>
  40.         </xs:complexContent>
  41.     </xs:complexType>
  42. </xs:schema>
…and XML document
Expand|Select|Wrap|Line Numbers
  1. <root xmlns="http://tempuri.org/schema.xsd">
  2.     <local name="a"/>
  3.     <local name="b">
  4.         <local name="a"/>
  5.     </local>
  6. </root>
It works and I’ve got notified that there is a duplicate key sequence. But what if I have an extension to this schema where I declared another element:
Expand|Select|Wrap|Line Numbers
  1. <root xmlns="http://tempuri.org/schema.xsd"
  2.       xmlns:e="http://tempuri.org/ext.xsd">
  3.     <local name="one">
  4.         <e:extended key="next"/>
  5.     </local>
  6.     <local name="two">
  7.         <local name="one"/>
  8.         <e:extended key="next"/>
  9.     </local>
  10. </root>
Expand|Select|Wrap|Line Numbers
  1. <xs:schema id="ext"
  2.     targetNamespace="http://tempuri.org/ext.xsd"
  3.     elementFormDefault="qualified"
  4.     xmlns="http://tempuri.org/ext.xsd"
  5.     xmlns:mstns="http://tempuri.org/ext.xsd"
  6.     xmlns:xs="http://www.w3.org/2001/XMLSchema"
  7.     xmlns:base="http://tempuri.org/schema.xsd"
  8. >
  9.  
  10.     <xs:import namespace="http://tempuri.org/schema.xsd"
  11.                schemaLocation="schema.xsd"/>
  12.  
  13.     <xs:element name="extended" type="TExtended" substitutionGroup="base:element">
  14.         <xs:unique name="extended-key-constraint">
  15.             <xs:selector xpath="I can't get to the root of the document here"/>
  16.             <xs:field xpath="@key"/>
  17.         </xs:unique>
  18.     </xs:element>
  19.  
  20.     <xs:complexType name="TExtended">
  21.         <xs:complexContent>
  22.             <xs:extension base="base:TElement">
  23.                 <xs:sequence>
  24.                     <xs:element ref="base:element" minOccurs="0" maxOccurs="unbounded"/>
  25.                 </xs:sequence>
  26.                 <xs:attribute name="key" use="required" type="xs:string"/>
  27.             </xs:extension>
  28.         </xs:complexContent>
  29.     </xs:complexType>
  30.  
  31. </xs:schema>
How can I set uniqueness to the element from the schema extensions?
Reply

Tags
schema, uniqueness