【问题标题】:XML Schema: Uniqueness constraintsXML 模式:唯一性约束
【发布时间】:2012-10-09 18:18:43
【问题描述】:

经过几个小时的尝试,我仍然无法让这个简单的例子做我想要的。目标很简单:只有为每个 Node 分配一个唯一的 NoteID 时,带有 Notes 的 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>

还有Notes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Notes  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
            xmlns:ns0='http://xml.netbeans.org/schema/Notes'
            xsi:schemaLocation='http://xml.netbeans.org/schema/Notes Notes.xsd'>
    <ns0:Note>
        <ns0:NoteID>1</ns0:NoteID>
        <ns0:Content>this</ns0:Content>
    </ns0:Note>
    <ns0:Note>
        <ns0:NoteID>1</ns0:NoteID>
        <ns0:Content>is a</ns0:Content>
    </ns0:Note>
    <ns0:Note>
        <ns0:NoteID>3</ns0:NoteID>
        <ns0:Content>test</ns0:Content>
    </ns0:Note>
</ns0:Notes>

我不知道为什么会这样:

$ xmllint --noout -schema Notes.xsd Notes.xml 
Notes.xml validates

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    xsd:unique 放错了位置,您需要在 xpaths 中明确命名空间前缀。

    这行得通:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      相关资源
      最近更新 更多