XML 属性唯一性
XML Attribute Uniqueness
我想强制执行唯一属性。我阅读了很多指南并尝试了很多配置,但我似乎无法理解 it.I 使用了两个不同的验证器,XML Copy Editor 和 http://www.xmlvalidation.com/。我做错了什么?
这里是被认为有效的xml:
<?xml version="1.0"?>
<stuff xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com test.xsd">
<thing att="hi">
<test>Hi</test>
</thing>
<thing att="hi">
<test>Hi</test>
</thing>
</stuff>
架构如下:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="stuff">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="xs:string"/>
</xs:sequence>
<xs:attribute name="att" type="xs:string"/>
</xs:complexType>
<xs:unique name="thing">
<xs:selector xpath="thing"/>
<xs:field xpath="@att"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
要正确验证您的 XML 文档,您必须执行以下操作:
- 将
xs:unique
声明移动到 stuff
元素声明(thing
元素没有名为 thing
的子元素;XPath 表达式是相对于父元素的).
- 使用命名空间前缀(当您想在 XSD 中使用 XPath 时,不要使用默认命名空间)。
你的 XSD 应该改写如下:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:p="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="stuff">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="xs:string"/>
</xs:sequence>
<xs:attribute name="att" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="thing">
<xs:selector xpath="p:thing"/>
<xs:field xpath="@att"/>
</xs:unique>
</xs:element>
</xs:schema>
我想强制执行唯一属性。我阅读了很多指南并尝试了很多配置,但我似乎无法理解 it.I 使用了两个不同的验证器,XML Copy Editor 和 http://www.xmlvalidation.com/。我做错了什么?
这里是被认为有效的xml:
<?xml version="1.0"?>
<stuff xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com test.xsd">
<thing att="hi">
<test>Hi</test>
</thing>
<thing att="hi">
<test>Hi</test>
</thing>
</stuff>
架构如下:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="stuff">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="xs:string"/>
</xs:sequence>
<xs:attribute name="att" type="xs:string"/>
</xs:complexType>
<xs:unique name="thing">
<xs:selector xpath="thing"/>
<xs:field xpath="@att"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
要正确验证您的 XML 文档,您必须执行以下操作:
- 将
xs:unique
声明移动到stuff
元素声明(thing
元素没有名为thing
的子元素;XPath 表达式是相对于父元素的). - 使用命名空间前缀(当您想在 XSD 中使用 XPath 时,不要使用默认命名空间)。
你的 XSD 应该改写如下:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:p="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="stuff">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="xs:string"/>
</xs:sequence>
<xs:attribute name="att" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="thing">
<xs:selector xpath="p:thing"/>
<xs:field xpath="@att"/>
</xs:unique>
</xs:element>
</xs:schema>