XML Schema Question

B

bmichel

Regarding the XSD schema shown below, I want to modifiy it so that:
- the "owner_id" attribute in the "dog" element to exist in one of the
"owner" element "id" attribute.
- the "id" attribute in the "owner" element is unique.

How should I modify the XSD?

Examples (in the xml):
Example 1 should be validated
== Example 1 ==
<petshop>
<owners>
<owner id="123">
<name>James Bond</name>
</owner>
</owners>

<dogs>
<dog owner_id="123">
<name>Popeye</name>
</dog>
</dogs>
</petshop>
== End Example 1 ==

Example 2 should not be validated, because id 765 is not that of an
owner
== Example 2 ==
<petshop>
<owners>
<owner id="123">
<name>James Bond</name>
</owner>
</owners>

<dogs>
<dog owner_id="765">
<name>Popeye</name>
</dog>
</dogs>
</petshop>
== End Example 2 ==

Example 3 should not be validated because two owner have the same id
== Example 3 ==
<petshop>
<owners>
<owner id="123">
<name>James Bond</name>
</owner>
<owner id="123">
<name>Money Penny</name>
</owner>
</owners>

<dogs>
<dog owner_id="123">
<name>Popeye</name>
</dog>
</dogs>
</petshop>
== End Example 3 ==


======= XSD STARTS =======

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- Root: 'petshop' element -->
<xs:element name="petshop" type="petshop_type"/>

<!-- 'petshop' element type definition
Composed of one instance of each of the following elements: 'dogs',
'owners' -->
<xs:complexType name="petshop_type">
<xs:sequence>
<xs:element name="owners" type="owners_type"/>
<xs:element name="dogs" type="dogs_type"/>
</xs:sequence>
</xs:complexType>

<!-- 'owners' element type definition -->
<xs:complexType name="owners_type">
<xs:sequence>
<xs:element name="owner" type="owner_type" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"/>
</xs:complexType>

<!-- 'owner' element type definition -->
<xs:complexType name="owner_type">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"/>
</xs:complexType>

<!-- 'dogs' element type definition -->
<xs:complexType name="dogs_type">
<xs:sequence>
<xs:element name="dog" type="dog_type" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<!-- 'dog' element type definition -->
<xs:complexType name="dog_type">
<xs:sequence>
<xs:element name="dog_name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="owner_id" type="xs:positiveInteger"
use="required"/>
</xs:complexType>
</xs:schema>

======= XSD ENDS =======
 
P

p.lepin

Regarding the XSD schema shown below, I want to modifiy
it so that:
- the "owner_id" attribute in the "dog" element to exist
in one of the "owner" element "id" attribute.
- the "id" attribute in the "owner" element is unique.
<petshop>
<owners>
<owner id="123">
<name>James Bond</name>
</owner>
</owners>

<dogs>
<dog owner_id="123">
<name>Popeye</name>
</dog>
</dogs>
</petshop>

[extra examples skipped]

People would be more willing to help you if you eliminated
the obvious errors in your example. The Schema given does
not validate your examples.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
[...]

<!-- 'owners' element type definition -->
<xs:complexType name="owners_type">
<xs:sequence>
<xs:element name="owner" type="owner_type"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger"
use="required"/>

Right. I see no id attribute on your owners element.
</xs:complexType>
[...]

<!-- 'dog' element type definition -->
<xs:complexType name="dog_type">
<xs:sequence>
<xs:element name="dog_name" type="xs:string"/>

Where's the dog_name element in your examples?
</xs:sequence>
<xs:attribute name="owner_id"
type="xs:positiveInteger"
use="required"/>
</xs:complexType>
</xs:schema>

W3C's XML Schema Primer SE 5.1-5.2 describes precisely
what you seem to need:

<xs:element name="petshop" type="petshop_type">
<xs:unique name="owner-id">
<xs:selector xpath="owners/owner"/>
<xs:field xpath="@id"/>
</xs:unique>
<xs:keyref name="pet-owner-id" refer="owner-id">
<xs:selector xpath="dogs/dog"/>
<xs:field xpath="@owner_id"/>
</xs:keyref>
</xs:element>

pavel@debian:~/dev/schema$ xmllint --schema ps2.xsd `s
ps*.xml` >/dev/null
ps1.xml validates
ps2.xml:9: Schemas validity error : Element 'dog': No
match found for key-sequence ['765'] of keyref
'pet-owner-id'.
ps2.xml fails to validate
ps3.xml:6: element owner: Schemas validity error : Element
'owner': Duplicate key-sequence ['123'] in unique
identity-constraint 'owner-id'.
ps3.xml fails to validate
pavel@debian:~/dev/schema$
 
B

bmichel

Sorry about the two errors you mentioned...
'owners' element shouldn't have an 'id' attribute (modified in xsd)
'dog_name' instead of 'name' (modified in xml examples)

Anyway, I was able to create the desired effect following your comment.
You were very helpful, thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top