Re: Question about uniqueness

Hi,

> <?xml version="1.0" encoding="ISO-8859-1"?>
> <Site xmlns="http://n-side/site/ssShema"
>             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>           xsi:schemaLocation="http://n-side/site/ssShema 
> ../schema/ssSchema.xsd">
> <Home name="CorHome">
>         <Topic name="Your_Needs">
>             <View name="Choice_1"/>
>             <View name="CorHome"/>
>             <View name="Your_Needs2"/>
>         </Topic>
>         <Topic name="Your_Needs2">
>             <View name="Choice_3"/>
>             <View name="Choice_4"/>
>             <View name="Choice_3"/>
>         </Topic>
>         <Topic name="Your_Needs"/>
>     </Home>
>     </Site>
>
> I would like to have a schema that enforces the uniqueness of all the 
> "name" attributes (for Home, Topic and View elements). The above 
> document is thus not valid (at all).
>
> I inserted the following XML Schema fragment within the Site element 
> definition:
>
>         <unique name="idName">       
>             <selector xpath=".//*"/>
>             <field xpath="@name"/>
>         </unique>
>
> But it is not working : there is no validation error for the above 
> document. 

What validator are you using?
The above look perfectly fine with me and validates with XML Spy 4.4 and 
MSXML4. I tried this with Xerces 2.0.1 but it didn't pick up on the 
error so that's a bug. I haven't tried with the latest 2.1.0 so 
hopefully this is fixed in the new version.

> Note that with the following fragment, it's OK, I have an error 
> because of "Choice_3" appearing twice.
>
>         <unique name="idName">       
>             <selector xpath=".//View"/>
>             <field xpath="@name"/>
>         </unique>
>
> and I have the uniqueness of the name in all the View pages

I am, however, very surprised that you got the above to work. Note that 
your instance document use a default namespace which means that all you 
elements are declared in the "http://n-side/site/ssShema" namespace. 
Since XPath doesn't support the default namespace the above uniqueness 
check would never match any elements and should never give you an error. 
For this to work you must explicitly declare a namespace prefix for 
the "http://n-side/site/ssShema" namespace in your schema document and 
use that in the XPath expression:

<xs:schema ... xmlns:s="http://n-side/site/ssShema" ...>
....
        <unique name="idName">       
            <selector xpath=".//s:View"/>
            <field xpath="@name"/>
        </unique>
...
</xs:schema>

Cheers,
/Eddie

Received on Tuesday, 10 September 2002 20:05:44 UTC