[Home] [By Thread] [By Date] [Recent Entries]

  • To: XML DEV <xml-dev@l...>
  • Subject: schema validate key uniqueness attribute (name) of elements
  • From: Pedro Salazar <pedro-b-salazar@p...>
  • Date: 20 Feb 2004 16:52:14 +0000
  • Organization: Portugal Telecom Inovação, S.A.

Greetings,

I intend to create a schema where I could validate that an element would
have children elements but the name attribute should be unique, or more
than that, be a key! 

I've tried to define it with the element key ("service") but my tests
failed. I mean, my parser (Xerces2) and oxygen (ide tool) let pass that
constraint in my schema.

My question is if I have my schema well defined for that purpose. I put
the schema and test xml sample that should fail because I have two
elements with the same name ('service1').

Any ideas?

thanks,
Pedro Salazar.

Test case:

======= SCHEMA =========
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:med="http://med/" targetNamespace="http://med/">
    <xs:element name="serviceList" type="med:serviceListType">
        <xs:key name="service">
            <xs:selector xpath="serviceImport/serviceExport"/>
            <xs:field xpath="@name"/>
        </xs:key>
    </xs:element>
    <xs:element name="serviceImport" type="med:serviceImportType"/>
    <xs:element name="serviceExport" type="med:serviceExportType"/>
   <xs:complexType name="serviceImportType">
        <xs:attribute name="name" use="required" type="xs:string"/>
    </xs:complexType>
    <xs:complexType name="serviceExportType">
        <xs:attribute name="name" use="required" type="xs:string"/>
    </xs:complexType>
</xs:schema>

========= XML ==============
<?xml version="1.0" encoding="ISO-8859-1"?>
<serviceList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://med/ file:/home/psalazar/med.xsd"
    xmlns="http://med/" name="test" version="1.0">
    <serviceImport name="service1"/>
    <serviceExport name="service1"/>
</serviceList>

-- 
PS
pedro-b-salazar_at_ptinovacao_dot_pt
PGP:0E129E31D803BC61


Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member