473,387 Members | 1,520 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,387 software developers and data experts.

XMLSchema: key/keyref

Hi!

I'm trying to accomplish the following:

<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='teaser.xsd'>

<b>
<a>John</a>
<a>Paul/a>
<a>George</a>
<a>Ringo/a>
<b>

<c>
<a>John</a>
<a>Paul</a>
<a>Ringo</a>
</c>

<c>
<a>John</a>
<a>Paul</a>
<a>George</a>
</c>

</root>

I want to make sure not only, that every c/a is equal to one of b/a.
(I can make c/a/. the keyref of b/a)
But also every b/a should be present in every c/a.

To simplify it:
Every c/a/. should have an correspondent b/a/.

Any help would be appreciated.
Thanks.
--
Roland Prähofer
Feb 15 '06 #1
6 1781
Hi,

If b/a values are unique then you can define that as a key and the c/a
values as key references. If they are not unique then you cannot do
that with XML Schema alone but you can solve that with XML Schema and
embedded Schematron rules.

Here it is a sample schema for the first case when b/a values are
unique:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element maxOccurs="unbounded" ref="c"/>
</xs:sequence>
</xs:complexType>
<xs:key name="aOfb">
<xs:selector xpath="b/a"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref refer="aOfb" name="aOfc">
<xs:selector xpath="c/a"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="xs:NCName"/>
</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Feb 16 '06 #2
Hi George!

Thanks for your reply!

As I wrote, there's no problem in making c/a a keyref for b/a.
Also b/a values _are_ unique.

I have to make sure though, that _all_ b/a's are also in c/a,
which is not guarranteed here.

All the Best,

Roland

-----------

<b>
<a>John</a>
<a>Paul/a>
<a>George</a>
<a>Ringo/a>
<b>

<c>
<a>John</a>
<a>Paul</a>
<a>Ringo</a>
</c>

<c>
<a>John</a>
<a>Paul</a>
<a>George</a>
</c>

Hi,

If b/a values are unique then you can define that as a key and the c/a
values as key references. If they are not unique then you cannot do
that with XML Schema alone but you can solve that with XML Schema and
embedded Schematron rules.

Here it is a sample schema for the first case when b/a values are
unique:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element maxOccurs="unbounded" ref="c"/>
</xs:sequence>
</xs:complexType>
<xs:key name="aOfb">
<xs:selector xpath="b/a"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref refer="aOfb" name="aOfc">
<xs:selector xpath="c/a"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="xs:NCName"/>
</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


--
memeticdesign
Roland Praehofer
T: 030 827 029 77
E: rp@memeticdesign.de
W: www.memeticdesign.de
Feb 16 '06 #3
Hi Roland,

Then you will need Schematron to test that for an b/a there is at least
a c/a with the same value. Here it is a complete working schema that
does that:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element maxOccurs="unbounded" ref="c"/>
</xs:sequence>
</xs:complexType>
<xs:key name="aOfb">
<xs:selector xpath="b/a"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref refer="aOfb" name="aOfc">
<xs:selector xpath="c/a"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="xs:NCName">
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check that b/a has a c/a with the same
value.">
<sch:rule context="a[parent::b]">
<sch:assert test=".=../../c/a">There is no c/a with the
same value.</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:schema>

On the following document

<root>

<b>
<a>John</a>
<a>Paul</a>
<a>George</a>
<a>Ringo</a>
<a>NotHere</a>
</b>

<c>
<a>John</a>
<a>Paul</a>
<a>Ringo</a>
</c>

<c>
<a>John</a>
<a>Paul</a>
<a>George</a>
</c>

</root>

I get:

SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 8:0
Description: There is no c/a with the same value. (.=../../c/a)
where line 8 is <a>NotHere</a>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Feb 16 '06 #4
Hi George !

Thanks a lot!
Never used Schematron before, so some work has to be done.
It looks like it's the solution for my problem, though.

Thanks,

Roland
Hi Roland,

Then you will need Schematron to test that for an b/a there is at least
a c/a with the same value. Here it is a complete working schema that
does that:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element maxOccurs="unbounded" ref="c"/>
</xs:sequence>
</xs:complexType>
<xs:key name="aOfb">
<xs:selector xpath="b/a"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref refer="aOfb" name="aOfc">
<xs:selector xpath="c/a"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="a"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="xs:NCName">
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check that b/a has a c/a with the same
value.">
<sch:rule context="a[parent::b]">
<sch:assert test=".=../../c/a">There is no c/a with the
same value.</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:schema>

On the following document

<root>

<b>
<a>John</a>
<a>Paul</a>
<a>George</a>
<a>Ringo</a>
<a>NotHere</a>
</b>

<c>
<a>John</a>
<a>Paul</a>
<a>Ringo</a>
</c>

<c>
<a>John</a>
<a>Paul</a>
<a>George</a>
</c>

</root>

I get:

SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 8:0
Description: There is no c/a with the same value. (.=../../c/a)
where line 8 is <a>NotHere</a>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


--
memeticdesign
Roland Praehofer
T: +49 030 827 029 77
E: rp@memeticdesign.de
W: www.memeticdesign.de
Feb 16 '06 #5
Roland Praehofer writes:
Hi George!

Thanks for your reply!

As I wrote, there's no problem in making c/a a keyref for b/a.
Also b/a values _are_ unique.

I have to make sure though, that _all_ b/a's are also in c/a,
which is not guarranteed here.


So make b/a a keyref as well, to c/a as a key. This forces the two to
be identical ignoring order.

ht
--
Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
Half-time member of W3C Team
2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
Fax: (44) 131 650-4587, e-mail: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Feb 20 '06 #6
Hello Henry,

The problem as I understand from the OP example is that c/a values are
not unique, so they cannot form a key.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Feb 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: J E E | last post by:
Hi, Why can't I create a key and keyref with an attribute declared in an attribute group? Code below describes what I'm trying to accomplish. <!-- An attribute group --> <xs:attributeGroup...
0
by: mark.van.der.voort | last post by:
Hi, I have defined a simple key/keyref validation for describing a tree. <xs:key name="key_member"> <xs:selector xpath=".//member"/> <xs:field xpath="@id"/> </xs:key> <xs:keyref...
3
by: Mike | last post by:
In my XML document, I am defining a key on an element, which works fine. I also have the corresponding keyref element in place to enforce ref. integrity. The issue: it's not necessary that an...
2
by: Jaunedeau | last post by:
I have some XML that must look like this : <movies> <movie id="1"> <actor preferredMovieId="1"> <actor preferredMovieId="2"> <actor preferredMovieId="3"> </movie> <movie id="2"> <actor...
0
by: Kent Boogaart | last post by:
Hi, Suppose an XSD as follows: <xsd:complexType name="myType"> <xsd:complexContent> <xsd:extension base="myBaseType"> <xsd:sequence> <xsd:element name="thing" minOccurs="0"...
2
by: Kent Boogaart | last post by:
Hello, I have a complex type defined in its own schema as follows: <xsd:complexType name="myType"> <xsd:complexContent> <xsd:extension base="myBaseType"> <xsd:attribute name="parentId"...
4
by: jasonnerothin | last post by:
I'm trying to figure out exactly what's going wrong with an xpath keyref validation scenario. In brief, I have something like the following in my xsd: <xs:schema> <xs:complexType...
0
by: mavis | last post by:
Does IntellJ support the checking for "keyref" definition? Why it seems does not work at all? <xs:key name="supported-platforms-name-unique"> <xs:selector...
0
by: =?Utf-8?B?cm9zYW5h?= | last post by:
I have a xsd, schema get about a Microsoft tool using my own Database. I need modify in code the structure Keyref element, but don't add a standard item. I need add my own...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.