XSD唯一约束(XML Schema)

时间:2015-05-20 04:26:08

标签: xml validation xsd unique

我对使用XML架构的Unique约束有点麻烦。

以下是要验证的XML的一部分:

    <pieces>
    <whitePieces>
    <pawn   name="p" taken="false" POSonBoard="12"></pawn>
    <pawn   name="p" taken="false" POSonBoard="12"></pawn>
    <rook   name="r" taken="false" POSonBoard="11"></rook>
    <rook   name="r" taken="false" POSonBoard="81"></rook>
    <knight name="n" taken="false" POSonBoard="21"></knight>
    <knight name="n" taken="false" POSonBoard="71"></knight>
    <bishop name="b" taken="false" POSonBoard="31"></bishop>
    <bishop name="b" taken="false" POSonBoard="61"></bishop>
    <queen  name="q" taken="false" POSonBoard="41"></queen>
    <king   name="k" taken="false" POSonBoard="51"></king>
  </whitePieces>
  <blackPieces
(and you get what goes here I'm sure)

(注意顶部的两个棋子如何具有相同的POSonBoard值)

这是XSD的一部分(或者我应该发布整个事情吗?它非常冗长)

<xs:element name="blackPieces">
          <xs:complexType>
            <xs:sequence>

              <xs:element name="pawn" minOccurs="8" maxOccurs="8">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="P"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

              <xs:element name="rook" minOccurs="2" maxOccurs="10">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="R"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

              <xs:element name="knight" minOccurs="2" maxOccurs="10">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="N"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

              <xs:element name="bishop" minOccurs="2" maxOccurs="10">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="B"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

              <xs:element name="queen" minOccurs="1" maxOccurs="9">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="Q"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

              <xs:element name="king" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="K"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute ref="POSonBoard"></xs:attribute>
                </xs:complexType>
              </xs:element>

            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:all>
    </xs:complexType>
    <xs:unique name="testUnique">
      <xs:selector xpath="POSonBoard"/>
      <xs:field xpath="pieces"/>
    </xs:unique>
  </xs:element>

我甚至不能100%确定我将唯一约束放在正确的位置 (假设根元素和你有什么存在但是上面没有包含以节省一些空间,一切都很好[除了唯一约束之外])

如果有人能引导我朝着正确的方向前进,那就太棒了! (如果您发现其他任何事情都做得不好,请告诉我们)

1 个答案:

答案 0 :(得分:1)

我必须弄清楚它的来源才能让它发挥作用。请注意,为了简洁起见,我将原始代码减少了很多。

XML:

<?xml version="1.0" encoding="utf-8" ?>

<pieces xmlns="http://www.example.org/pieces" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/pieces XMLSchema1.xsd">
  <whitePieces>
    <pawn name="P" taken="false" POSonBoard="p1"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
    <pawn name="P" taken="false" POSonBoard="p2"></pawn>
  </whitePieces>
</pieces>

XSD:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/pieces" xmlns:tns="http://www.example.org/pieces" elementFormDefault="qualified">
  <xs:element name="pieces">
    <xs:complexType>
      <xs:all>
        <xs:element name="whitePieces">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="pawn" minOccurs="8" maxOccurs="8">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" fixed="P"></xs:attribute>
                  <xs:attribute name="taken" type="xs:boolean"></xs:attribute>
                  <xs:attribute name="POSonBoard" type="xs:string" />
               </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
          <xs:unique name="testUnique">
            <xs:selector xpath="tns:pawn" />
            <xs:field xpath="@POSonBoard" />
          </xs:unique>
        </xs:element>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

有趣的部分是选择器上的XPath值:

<xs:selector xpath="tns:pawn" />

并且已调整字段的XPath值以查看属性:

<xs:field xpath="@POSonBoard" />

看起来好像是名称空间。我希望有所帮助。