如何在 XML 中声明属性 ID [英] How to declare an attribute ID in XML
问题描述
我正在写一些 XML 和 XSD 作为作业...在我的 XML 中,我有一个名为 a
(不是实际名称)的标记和名为 id
的属性.我的部分 XML 如下所示:
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:
<a id="1">
...........
</a>
<a id="1">
............
</a>
当我使用 XSD 进行验证时,它不会出现错误....
When I validate using XSD it doesn't give an error....
<xsd:attribute name="id" type="xsd:string" />
我尝试使用 xsd:ID 作为属性 id
的数据类型,但它给了我一个错误;我无法弄清楚是什么问题.
I tried to use xsd:ID as a data type of attribute id
but it gave me an error; I couldn't figure out what the problem is.
我该怎么做?
推荐答案
您应该重新使用 type="xsd:ID"
.除了确保该值是唯一的之外,它还允许您使用 xsd:IDREF
进行引用.
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:IDREF
for referencing.
当您尝试使用 xsd:ID
时遇到的错误是 ID 值必须以字母开头.如果您将 ID 更改为ID-1"/ID-2"或a1"/a2"之类的内容,它会正常工作.
The error you're getting when you try to use xsd:ID
is 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: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>
示例 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>
这篇关于如何在 XML 中声明属性 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!