Specifying unqiueness of an attribute in xsd.
-
Hi All, I have an simple xml file for which I generated an xsd file using Altova XMLSpy tool. As I wanted the end user who makes the xml entry to enter an unique ID which is an attribute of one of the elements. I modified the generated xsd file to accomodate this rule by including "xs:unique" node and entered valid values(i believe)but to no avail. Below is my xml file.
<?xml version="1.0"?>
<wwdfdb:DDB xmlns:wwdfdb="http://www.xybec.com/ns/wwdfdb" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xybec.com/ns/wwdfdb Sample.xsd">
wwdfdb:MyList
<wwdfdb:Num id="3020">
wwdfdb:Ver1.0.0.1</wwdfdb:Ver>
wwdfdb:StatusInUse</wwdfdb:Status>
</wwdfdb:Num>
<wwdfdb:Num id="3021">
wwdfdb:Ver1.0.0.1</wwdfdb:Ver>
wwdfdb:StatusInUse</wwdfdb:Status>
</wwdfdb:Num>
</wwdfdb:MyList>
</wwdfdb:DDB>Below is the xsd file for the xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2009 sp1 (http://www.altova.com)-->
<xs:schema xmlns="http://www.xybec.com/ns/wwdfdb" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xybec.com/ns/wwdfdb">
<xs:element name="Ver">
xs:simpleType
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Status">
xs:simpleType
<xs:restriction base="xs:string">
<xs:enumeration value="InUse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Num">
xs:complexType
xs:sequence
<xs:element ref="Ver"/>
<xs:element ref="Status"/>
</xs:sequence>
<xs:attribute name="id" use="required">
xs:simpleType
<xs:restriction base="xs:short">
<!--<xs:enumeration value="3020"/>
<xs:enumeration value="3021"/>-->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="MyList">
xs:complexType
xs:sequence
<xs:element ref="Num" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--added below lines to achieve uniqueness for at -
Hi All, I have an simple xml file for which I generated an xsd file using Altova XMLSpy tool. As I wanted the end user who makes the xml entry to enter an unique ID which is an attribute of one of the elements. I modified the generated xsd file to accomodate this rule by including "xs:unique" node and entered valid values(i believe)but to no avail. Below is my xml file.
<?xml version="1.0"?>
<wwdfdb:DDB xmlns:wwdfdb="http://www.xybec.com/ns/wwdfdb" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xybec.com/ns/wwdfdb Sample.xsd">
wwdfdb:MyList
<wwdfdb:Num id="3020">
wwdfdb:Ver1.0.0.1</wwdfdb:Ver>
wwdfdb:StatusInUse</wwdfdb:Status>
</wwdfdb:Num>
<wwdfdb:Num id="3021">
wwdfdb:Ver1.0.0.1</wwdfdb:Ver>
wwdfdb:StatusInUse</wwdfdb:Status>
</wwdfdb:Num>
</wwdfdb:MyList>
</wwdfdb:DDB>Below is the xsd file for the xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2009 sp1 (http://www.altova.com)-->
<xs:schema xmlns="http://www.xybec.com/ns/wwdfdb" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xybec.com/ns/wwdfdb">
<xs:element name="Ver">
xs:simpleType
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Status">
xs:simpleType
<xs:restriction base="xs:string">
<xs:enumeration value="InUse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Num">
xs:complexType
xs:sequence
<xs:element ref="Ver"/>
<xs:element ref="Status"/>
</xs:sequence>
<xs:attribute name="id" use="required">
xs:simpleType
<xs:restriction base="xs:short">
<!--<xs:enumeration value="3020"/>
<xs:enumeration value="3021"/>-->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="MyList">
xs:complexType
xs:sequence
<xs:element ref="Num" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--added below lines to achieve uniqueness for atIn xmllint (libxml2's standalone app), I needed to add an explicit namespace specification for the elements in the unique spec:
<xs:unique name="MyUniqueID" xmlns:wwdfdb="http://www.xybec.com/ns/wwdfdb" > <xs:selector xpath="wwdfdb:Num"/> <xs:field xpath="@id"/> </xs:unique>
That did the right thing (validating uniqueness, rejecting sameness) in the limited testing I did!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
In xmllint (libxml2's standalone app), I needed to add an explicit namespace specification for the elements in the unique spec:
<xs:unique name="MyUniqueID" xmlns:wwdfdb="http://www.xybec.com/ns/wwdfdb" > <xs:selector xpath="wwdfdb:Num"/> <xs:field xpath="@id"/> </xs:unique>
That did the right thing (validating uniqueness, rejecting sameness) in the limited testing I did!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
It works.:thumbsup: Thanks a lot Stuart for the solution.