473,372 Members | 917 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,372 software developers and data experts.

XML schema restriction to values of another element

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?


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 5261
700 Expert 512MB
I'm afraid not in XML Schema. But you can define it in Schematron
May 6 '10 #2
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>
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

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 >

May 6 '10 #4
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
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

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...
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...
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...
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>...
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
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...
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
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...
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?...
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...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.