xsd中的唯一属性 [英] unique attribute in xsd

查看:169
本文介绍了xsd中的唯一属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我尝试将元素文件夹的属性"id"定义为唯一.从理论上讲,我知道该怎么做.我的问题是,我只能使用相同级别的元素才能达到此目的(英语级别正确吗?).请参见下面的代码.


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.


<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>



问了两个例子.首先获取有效的xml:



as asked two examples. first for a valid 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 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>



如您所见,属性"id"无论级别如何都应该是唯一的(应该已经提到了...对不起).

非常感谢!

olli



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

thx a lot!

olli

推荐答案

感谢您对我的评论做出了很好的改进,以回应我的评论.

这是通过将XSD元素<xsd:unique>与指定的 scope 一起使用来完成的.请在此处查看规格,说明和代码示例:
http://msdn.microsoft.com/en-us/library/ms256146.aspx [ ^ ].

—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


再次感谢...,现在可以进行验证.重要的部分是
Thanks again... validates now as it should. The important part is
<xs:selector xpath=".//*" />

解决方案如下:

Solution looks like this:

<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>


这篇关于xsd中的唯一属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆