473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 5264
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

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

Similar topics

3
by: Y.S. | last post by:
Hello All, I have the following schema: .... <xs:element name="data_type_id" type="xs:unsignedShort" /> <xs:element name="data_value" type="xs:anyType" /> .... What I want to do is to...
0
by: kelvSYC | last post by:
Is it possible in an XML schema to say that an attribute is valid only when it references another attribute in some other element (similar to IDREF but with strings)? For example, say I have an...
2
by: Gehirnloser Zombie | last post by:
Hello folks, Short question: Is it possible to express with XML-Schema that for an element some attributes may apear only disjunctivly (i.e. an element <E> can either carry the attribute "a1" OR...
0
by: Piers Chivers | last post by:
Hi, I need to restrict the allowed included elements dependent upon the value of another element. How do I do this using XML Schema? For example, the XML <Petshop> <Kind>Dog</Kind>...
3
by: abs | last post by:
Has anybody seen a function which checked if one of passed elements is nested in the node of another element passed to function, no matter how deep it is nested ? Thanks in advance, ABS
1
by: mflll | last post by:
How does one say in one schema that one wants an element defined in another schema. For example, I want to include in the Employee definition, an Address element defined in the schema...
5
by: felipevaldez | last post by:
how can I put an element below another element, that's not absolutely positioned? so I have a n element, called X, and next to that, an element Y X Y XXXXXX
1
by: cidney001 | last post by:
Hi, I am trying to create a schema with an inline schema in it. I have looked for a simple example of how to do this but i can't find one. I have included 2 simple schemas. How would i make the...
0
by: icesign | last post by:
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?...
35
Frinavale
by: Frinavale | last post by:
I have a quick question... Before I start looping through all of the child elements of a <div> I wanted to ask if there is a better way to check if an element is a child of another element? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.