XML模式:唯一性约束
经过几个小时的尝试,我仍然无法得到这个简单的例子做我想要的。目标非常简单:只有为每个XML模式:唯一性约束,xml,xsd,schema,Xml,Xsd,Schema,经过几个小时的尝试,我仍然无法得到这个简单的例子做我想要的。目标非常简单:只有为每个节点分配了唯一的NoteID时,带有注释的xml文档才有效 这是我的Notes.xsd: <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xml.netbeans.org/schem
节点
分配了唯一的NoteID
时,带有注释的xml文档才有效
这是我的Notes.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/Notes"
xmlns:tns="http://xml.netbeans.org/schema/Notes"
elementFormDefault="qualified">
<xsd:element name="Notes">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Note" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="NoteID" type="xsd:positiveInteger"/>
<xsd:element name="Content" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="newKey">
<xsd:selector xpath="."/>
<xsd:field xpath="NoteID"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我不知道为什么这证明了:
$ xmllint --noout -schema Notes.xsd Notes.xml
Notes.xml validates
xsd:unique
位于错误的位置,您需要在xpath
s中使用显式名称空间前缀
这项工作:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/Notes"
xmlns:tns="http://xml.netbeans.org/schema/Notes"
elementFormDefault="qualified">
<xsd:element name="Notes">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Note" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="NoteID" type="xsd:positiveInteger"/>
<xsd:element name="Content" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="newKey">
<xsd:selector xpath="tns:Note"/>
<xsd:field xpath="tns:NoteID"/>
</xsd:unique>
</xsd:element>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/Notes"
xmlns:tns="http://xml.netbeans.org/schema/Notes"
elementFormDefault="qualified">
<xsd:element name="Notes">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Note" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="NoteID" type="xsd:positiveInteger"/>
<xsd:element name="Content" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="newKey">
<xsd:selector xpath="tns:Note"/>
<xsd:field xpath="tns:NoteID"/>
</xsd:unique>
</xsd:element>
</xsd:schema>