bitbonk

Is there a way to prohibit the occurence of two elements with the same attrbiute value within the same parent For example the following should not validate because of the same isbn value:

Code Block

...

<books>

<book isbn ="0201485419" />

<book isbn ="0201485419" /> <!-- invalid -->

</books>

<books>

<book isbn ="0201485419" /> <!-- valid -->

</books>

...




Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

Martin Honnen

Here is a schema excerpt:

Code Block

<xs:element name="books" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="isbn" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="unique-isbn">
<xs:selector xpath="book"/>
<xs:field xpath="@isbn"/>
</xs:unique>
</xs:element>






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

bitbonk

Does the visual studio 2005 xml editor usually evaluate such keys, i.e. if I created a xml file using visual studio and selected the xsd to validate against, would I get error/warning messages about key constraints while editing the xml file






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

Martin Honnen

You need to associate the XML document that you edit with the schema, then you should get validation error messages.






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

bitbonk

Then I am probably still doing something wrong because it validates even if I have two book elements within the same parent with the same Isbn. I defined the books as global complex type just like so:

Code Block

<xs:complexType name="books">

<xs:sequence>

<xs:element name="Book" minOccurs="0" maxOccurs="unbounded">

<xs:complexType>

<xs:attribute name="Isbn" type="notEmptyString" use="required" />

</xs:complexType>

</xs:element>

</xs:sequence>

<xs:attribute name="SomeAttribute" type="notEmptyString" use="required" />

</xs:complexType>

...

<xs:element name="BooksParent" minOccurs="0" maxOccurs="unbounded">

<xs:complexType>

<xs:all>

<xs:element name="CoolBooks" type="books" minOccurs="0" maxOccurs="1">

<xs:unique name="unique-coolBooksIsbn">

<xs:selector xpath="Book" />

<xs:field xpath="@Isbn" />

</xs:unique>

</xs:element>

<xs:element name="HotBooks" type="books" minOccurs="0" maxOccurs="1">

<xs:unique name="unique-hotBooksIsbn">

<xs:selector xpath="Book" />

<xs:field xpath="@Isbn" />

</xs:unique>

</xs:element>

</xs:all>

<xs:attribute name="SomeOtherAttribute" type="notEmptyString" use="required" />

</xs:complexType>

</xs:element>

Do you see anything




Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

Martin Honnen

In your original post the element and attribute names (e.g. book, isbn) are all lower case while the schema defines names like Book or Isbn starting with an upper case letter. Based on that the schema you posted does not apply to the XML instance document.

Please provide a minimal but complete XML document and XSD schema where we can then suggest how to use/place the xs:unique constraint.






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

bitbonk

This XML:

Code Block

< xml version="1.0" encoding="utf-8" >

<BooksParent xmlns="http://tempuri.org/Books.xsd">

<CoolBooks SomeAttribute="cool">

<Book Isbn="123456"/>

<Book Isbn="123456"/>

</CoolBooks>

<HotBooks SomeAttribute="hot">

<Book Isbn="123456"/>

<Book Isbn="123456"/>

</HotBooks>

</BooksParent>

validates agains this schema in VS 2005:

Code Block

< xml version="1.0" encoding="utf-8" >

<xs:schema id="Books" targetNamespace="http://tempuri.org/Books.xsd"

elementFormDefault="qualified" xmlns="http://tempuri.org/Books.xsd"

xmlns:mstns="http://tempuri.org/Books.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="notEmptyString">

<xs:restriction base="xs:string">

<xs:minLength value="1" />

</xs:restriction>

</xs:simpleType>

<xs:complexType name="books">

<xs:sequence>

<xs:element name="Book" minOccurs="0" maxOccurs="unbounded">

<xs:complexType>

<xs:attribute name="Isbn" type="notEmptyString" use="required" />

</>xs:complexType>

</>xs:element>

</>xs:sequence>

<xs:attribute name="SomeAttribute" type="notEmptyString" use="required" />

</xs:complexType>

<xs:element name="BooksParent">

<xs:complexType>

<xs:sequence>

<xs:element name="CoolBooks" type="books">

<xs:unique name="unique-CoolBooks">

<xs:selector xpath="Book" />

<xs:field xpath="@Isbn" />

</xs:unique>

</xs:element>

<xs:element name="HotBooks" type="books">

<xs:unique name="unique-HotBooks">

<xs:selector xpath="Book" />

<xs:field xpath="@Isbn" />

</xs:unique>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

This shouldn't happen because of the unique constraints. ( )






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

Martin Honnen

The problem is the namespace the elements are in, for that you need to adapt the XPath expressions e.g

Code Block

<xs:selector xpath="mstns:Book"/>

The xs:field is fine as the targetNamespace does only apply to the elements your schema defines.






Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

bitbonk

Why are the elements in the namespace mstn There is also a xmlns="http://tempuri.org/Books.xsd" definition as the default namespace. Does xmlns:mstns="http://tempuri.org/Books.xsd" override this Why did Visual Studio generate this additonal mstns namespace definition anyway




Re: XML and the .NET Framework unique constraint in xml attribute with xml schema

Martin Honnen

The elements your schema defines are in the targetNamespace of the schema as the schema defines a targetNamespace and as it says elementFormDefault="qualified". The name of the namespace is not mstns but rather the URL http://tempuri.org/Books.xsd.

mstns is just a prefix bound to the namespace URI and XPath needs a prefix to select elements in a namespace.