Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hello everyone,

i try to define the attribute "id" of the element folder as unique. theoretically i know how to do this. my problem is, that i can achive this only with elements on the same level (is level correct in english?). see the code below.


XML
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

	<xs:element name="root" type="rootType" />
	
	<xs:complexType name="rootType">
		<xs:sequence>
			<xs:element name="folder" type="folderType" maxOccurs="unbounded" />
		</xs:sequence>
		<xs:attribute name="noNamespaceSchemaLocation" type="xs:string" use="required" />
	</xs:complexType>
	
	<xs:complexType name="folderType">
		<xs:sequence>
			<!--
			element folder: subfolder
			-->
			<xs:element name="folder" type="folderType" minOccurs="0" maxOccurs="unbounded" />
		</xs:sequence>
		<!--
		attribute id: links items to folders (see catalogue.xsd or cataloge.xml)
		-->
		<xs:attribute name="id" type="xs:int" />
		
		<xs:attribute name="de-DE" type="xs:string" />
		<xs:attribute name="en-US" type="xs:string" />
		<!--
		attribute readonly: some folders can only be removed by changing the XML
		(i.e. non user generated or standard folders)
		-->
		<xs:attribute name="readonly" type="xs:boolean" />
		
	</xs:complexType>
	
</xs:schema>


as asked two examples. first for a valid xml:

XML
<?xml version="1.0"?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="structure.xsd">

	<folder id="0" de-DE="DocView" en-US="DocView" readonly="true">	
		<folder id="1" de-DE="Suche" en-US="Search" readonly="true">
			<folder id="2" de-DE="Trefferliste" en-US="Hit list" readonly="true"/>
			<folder id="3" de-DE="Suchhistorie" en-US="Search history" readonly="true"/>
			<folder id="4" de-DE="Zuletzt angezeigte Dokumente" en-US="Recently viewed documents" readonly="true"/>
		</folder>
	</folder>
</root>


second one that is not:

XML
<?xml version="1.0"?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="structure.xsd">

	<folder id="1" de-DE="DocView" en-US="DocView" readonly="true">	
		<folder id="1" de-DE="Suche" en-US="Search" readonly="true">
			<folder id="1" de-DE="Trefferliste" en-US="Hit list" readonly="true"/>
			<folder id="3" de-DE="Suchhistorie" en-US="Search history" readonly="true"/>
			<folder id="4" de-DE="Zuletzt angezeigte Dokumente" en-US="Recently viewed documents" readonly="true"/>
		</folder>
	</folder>
</root>


as you can see, the attribute "id" should be unique regardless the level (should have mentioned it already... sorry).

thx a lot!

olli
Posted
Updated 23-May-12 20:18pm
v3
Comments
Sergey Alexandrovich Kryukov 23-May-12 12:20pm    
The English "level" is good enough and understandable in this content (another word is "depth"), but the required uniqueness constraint itself is not fully clear. You commented on read-only, which is completely different. Could you explain it in detail in the following way: 1) scope of uniqueness; 2) create the simplest possible XSD sample without constraint (put is informally as a comment) and show XML (not XSD) samples; both satisfying the existing XSD, but one of them not satisfying the constraint and explain why.
--SA
hornet_79 24-May-12 2:10am    
improved the question with two example xmls and made clear which attribute should be unique. thx for your help!
Sergey Alexandrovich Kryukov 24-May-12 3:04am    
Thank you; now you've defined the problem well enough, as far as I can see, so I voted 5 for the clear question.
I hope my answer will help you.
--SA

Thank you for responding to my comment with the good improvement in the formulation of the question.

This is done by using the XSD element <xsd:unique> with specified scope. Please see the specification, explanation and the code sample here:
http://msdn.microsoft.com/en-us/library/ms256146.aspx[^].

—SA
 
Share this answer
 
Comments
Wendelius 24-May-12 15:13pm    
And a clear answer.
Sergey Alexandrovich Kryukov 24-May-12 15:14pm    
Thank you, Mika.
--SA
Thanks again... validates now as it should. The important part is
XML
<xs:selector xpath=".//*" />
Solution looks like this:

XML
<xs:schema attributeformdefault="qualified" elementformdefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

	<xs:element name="root" type="rootType">

		<xs:unique name="uniqueID">
			<xs:selector xpath=".//*" />
			<xs:field xpath="@id" />
		</xs:unique>	

        </xs:element>
	
	<xs:complextype name="rootType">
		<xs:sequence>
			<xs:element name="folder" type="folderType" maxoccurs="unbounded" />
		</xs:sequence>
		<xs:attribute name="noNamespaceSchemaLocation" type="xs:string" use="required" />
	</xs:complextype>
	
	<xs:complextype name="folderType">
		<xs:sequence>
			<!--
			element folder: subfolder
			-->
			<xs:element name="folder" type="folderType" minoccurs="0" maxoccurs="unbounded" />
		</xs:sequence>
		<!--
		attribute id: links items to folders (see catalogue.xsd or cataloge.xml)
		-->
		<xs:attribute name="id" type="xs:int" />		
		<xs:attribute name="de-DE" type="xs:string" />
		<xs:attribute name="en-US" type="xs:string" />
		<!--
		attribute readonly: some folders can only be removed by changing the XML
		(i.e. non user generated or standard folders)
		-->
		<xs:attribute name="readonly" type="xs:boolean" />
		
	</xs:complextype>
	
</xs:schema>
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-May-12 15:16pm    
If you are talking to me, you are welcome.
Will you accept my answer formally then (green button)? -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900