XML Schema重复元素值

时间:2014-07-22 15:31:53

标签: xml xsd xml-validation

假设我有一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Tree xmlns="http://www.w3schools.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Leaf>
        <Address>0.0.1.0</Address>
      </Leaf>
      <Leaf>
        <Address>0.0.1.1</Address>
      </Leaf>
      <Leaf>
        <Address>0.0.1.2</Address>
      </Leaf>
</Tree>

如何使用xsd文件确保没有两个具有相同地址的叶子?香港专业教育学院尝试使用标签没有运气。这就是我的xsd文件目前的样子:

<?xml version="1.0" encoding="UTF-8"?>
<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="Tree">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Leaf">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Address"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>

    <xs:unique name="address-name-is-unique">
      <xs:selector xpath="Leaf"/>
      <xs:field xpath="Address"/>

    </xs:unique>
  </xs:element>
</xs:schema>

如果碰巧发生类似这样的事情,我试图这样做:

<?xml version="1.0" encoding="utf-8"?>
<Tree xmlns="http://www.w3schools.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Leaf>
    <Address>0.0.1.0</Address>
  </Leaf>
  <Leaf>
    <Address>0.0.1.1</Address>
  </Leaf>
  <Leaf>
    <Address>0.0.1.1</Address>
  </Leaf>
</Tree>

它会出错。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您已经关闭,但您忽略了Leaf和Address位于命名空间中,因此在XPath表达式中,名称需要使用绑定到该命名空间的前缀进行限定。