469,270 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

XML schema restriction to values of another element

3
I would like to know if it is possible to write an XML Schema restriction that binds to values of another element.

For example I have XML
Expand|Select|Wrap|Line Numbers
  1. <people>
  2.     <family name="Smith">
  3.         <origin>England</origin>
  4.     </family>
  5.     <family name="Jain">
  6.         <origin>India</origin>
  7.     </family>
  8.     <family name="DelSol">
  9.         <origin>Spain</origin>
  10.     </family>
  11.     <person>
  12.          <Given>Mary</Given>
  13.          <Family>Smith</Family>
  14.     </person>
  15.     <person>
  16.          <Given>Jorege</Given>
  17.          <Family>DelSol</Family>
  18.     </person>
  19.     <person>
  20.          <Given>Sundeep</Given>
  21.          <Family>Jain</Family>
  22.     </person>
  23.     <person>
  24.          <Given>John</Given>
  25.          <Family>Farley</Family>
  26.     </person>
  27. </people>
In the following XML Schema I would like to restrict the Family value of a person to one of the name attributes of the family elements. This should complain on the last person as the Family name was not listed as a family element.

Is it possible to do so?

Thanks
Mark

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.   <xs:element name="people">
  4.     <xs:complexType>
  5.       <xs:sequence>
  6.         <xs:element maxOccurs="unbounded" name="family">
  7.           <xs:complexType>
  8.             <xs:sequence>
  9.               <xs:element name="origin" type="xs:string" />
  10.             </xs:sequence>
  11.             <xs:attribute name="name" type="xs:string" use="required" />
  12.           </xs:complexType>
  13.         </xs:element>
  14.         <xs:element maxOccurs="unbounded" name="person">
  15.           <xs:complexType>
  16.             <xs:sequence>
  17.               <xs:element name="Given" type="xs:string" />
  18.               <xs:element name="Family" type="xs:string" />
  19.             </xs:sequence>
  20.           </xs:complexType>
  21.         </xs:element>
  22.       </xs:sequence>
  23.     </xs:complexType>
  24.   </xs:element>
  25. </xs:schema>
May 5 '10 #1
5 4988
rski
700 Expert 512MB
I'm afraid not in XML Schema. But you can define it in Schematron
May 6 '10 #2
jkmyoung
2,057 Expert 2GB
Yes you can, assuming no 2 family names are the same.

Put this at the end of your people node:
Expand|Select|Wrap|Line Numbers
  1. <xs:keyref name="familyNameRef" refer="familyName">
  2.     <xs:selector xpath="person"/>
  3.     <xs:field xpath="Family"/>
  4. </xs:keyref>
  5. <xs:key name="familyName">
  6.     <xs:selector xpath="family"/>
  7.     <xs:field xpath="@name"/>
  8. </xs:key>
  9.  
Your validator should throw an error like:
cvc-identity-constraint.4.3: Key 'familyNameRef' with value 'Farley' not found for identity constraint of element 'people'.
May 6 '10 #3
mruedy
3
@jkmyoung
jkmyoung,

With your snippet, I get this

~/family > xmllint --noout --schema people.xsd people.xml
Unimplemented block at xmlschemas.c:5588
Unimplemented block at xmlschemas.c:5588
people.xml validates
~/family >


I put the block just before the </xs:element> (end tag) for people. Is that the correct location? I am using:

~/family > xmllint --version
xmllint: using libxml version 20616
compiled with: DTDValid FTP HTTP HTML C14N Catalog XPath XPointer XInclude Iconv Unicode Regexps Automata Schemas
~/family >

Thanks
May 6 '10 #4
jkmyoung
2,057 Expert 2GB
I didn't know you were using xmllint. XML Schema support in libxml2 is quite incomplete.

I've heard there is a newer version of xmllint, but am not sure. Try:
http://www.xmlsoft.org/ to find an updated version.
May 6 '10 #5
mruedy
3
@jkmyoung
Is there a non-network based (local executable) validator that will work with this?
May 6 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by kelvSYC | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.