如何在 XML 中声明属性 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9876959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare an attribute ID in XML
提问by Zeemaan
I'm writing some XML and an XSD as an assignment...
In my XML i have a tag called a( not actual name) and attribute called id. Part of my XML is shown below:
我正在编写一些 XML 和 XSD 作为作业...在我的 XML 中,我有一个名为a(不是实际名称)的标记和名为id. 我的 XML 的一部分如下所示:
<a id="1">
...........
</a>
<a id="1">
............
</a>
When I validate using XSD it doesn't give an error....
当我使用 XSD 进行验证时,它不会出现错误....
<xsd:attribute name="id" type="xsd:string" />
I tried to use xsd:ID as a data type of attribute idbut it gave me an error; I couldn't figure out what the problem is.
我尝试使用 xsd:ID 作为属性的数据类型,id但它给了我一个错误;我无法弄清楚问题是什么。
How can I do this?
我怎样才能做到这一点?
回答by Daniel Haley
You should go back to using type="xsd:ID". What this does in addition to making sure that the value is unique is that it will also allow you to use xsd:IDREFfor referencing.
你应该回去使用type="xsd:ID". 除了确保该值是唯一的之外,它还允许您xsd:IDREF用于引用。
The error you're getting when you try to use xsd:IDis that an ID value must start with a letter. If you change your ID's to something like "ID-1"/"ID-2" or "a1"/"a2", it will work fine.
尝试使用时遇到的错误xsd:ID是 ID 值必须以字母开头。如果您将 ID 更改为“ID-1”/“ID-2”或“a1”/“a2”之类的内容,它将正常工作。
Example Schema:
示例架构:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="doc">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" ref="a"/>
<xsd:element maxOccurs="unbounded" ref="b"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="a">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="id" use="required" type="xsd:ID"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="b">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="idref" use="required" type="xsd:IDREF"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Example XML:
示例 XML:
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Untitled1.xsd">
<a id="ID-1">
...........
</a>
<a id="ID-2">
............
</a>
<b idref="ID-1"/>
</doc>
回答by Alexey Pomelov
"1" is a valid string, so validation does not return an error. If you want to specify some restriction (e.g. "id should starts with a letter"), you have to declare your type and specify the pattern:
“1”是有效字符串,因此验证不会返回错误。如果你想指定一些限制(例如“id 应该以字母开头”),你必须声明你的类型并指定模式:
<xsd:simpleType name="myID">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-zA-Z].*"/>
</xsd:restriction>
</xsd:simpleType>
....
<xsd:attribute name="id" type="myID"/>
....
If you want to specify the uniqueness restriction, you can use the xsd:unique element like this:
如果要指定唯一性限制,可以像这样使用 xsd:unique 元素:
<xsd:element name="root" type="myList">
<xsd:unique name="myId">
<xsd:selector xpath="./a"/>
<xsd:field xpath="@id"/>
</xsd:unique>
</xsd:element>
This will mean that element "root" declared as some "myList" should contain subelements "a" with unique attributes "id"
这意味着声明为某些“myList”的元素“root”应该包含具有唯一属性“id”的子元素“a”

