Groups | Blog | Home
all groups > dotnet xml > july 2006 >

dotnet xml : xsd:unique - validate attriubte value for whole


Miguel Isidoro
7/27/2006 8:19:03 AM
Hi all,

I am trying to include a xsd:unique element in my schema to make an
attribute of an element unique at the whole document level.

Consider the following xml fragment:

<Form>
<Question>
<Response id="response1">
</Response>
<Response id="response1">
</Response>
</Question>
<Question>
<Response id="response1">
</Response>
</Question>
</Form>

Here is a fragment of my schema:

<xs:complexType name="Question">
<xs:sequence>
<xs:element minOccurs="0" name="Response" type="Response">
</xs:element>
<xs:unique name="questionResponseNameUnique">
<xs:selector xpath="Response" />
<xs:field xpath="@id" />
</xs:unique>

The problem with my schema is that it only validates the duplicate id
<Response> elements inside the same <Question> element, not at the whole
document level as I want. Is there any way to achieve this?

Thanks
Martin Honnen
7/27/2006 8:06:05 PM


[quoted text, click to view]


[quoted text, click to view]

Why don't you simply type that attribute as type xs:ID?

If you want to use the xs:unique element then you need to put it in the
definition of the root element (e.g. the Form element).


--

Martin Honnen --- MVP XML
Miguel Isidoro
7/28/2006 3:06:02 AM
Problem solved. Thanks for the tip. I had already tried that but with the
wrong syntax. Here is the example I have:

<xs:key name="responseNameKey">
<xs:selector xpath="./QuestionList/Question/ResponseList/Response |
../QuestionList/Question/ResponseList/Response/ResponseOptions/ResponseOption
|
../QuestionList/Question/QuestionGroupList/QuestionGroup/ResponseList/Response
|
../QuestionList/Question/QuestionGroupList/QuestionGroup/ResponseList/Response/ResponseOptions/ResponseOption
|
../QuestionList/Question/QuestionGroupList/QuestionGroup/QuestionList/Question/ResponseList/Response
|
../QuestionList/Question/QuestionGroupList/QuestionGroup/QuestionList/Question/ResponseList/Response/ResponseOptions/ResponseOption" />
<xs:field xpath="@name" />
</xs:key>

It is this complicated because not all elements have an attribute that
should be considered an unique key, so I enumerated the elements that have
the attribute that can't be repeated. I included this schema fragment at the
root level.

Tnanks a lot
Miguel


[quoted text, click to view]
AddThis Social Bookmark Button