【问题标题】:XSD Unique constraint (XML Schema)XSD 唯一约束(XML 模式)
【发布时间】:2015-05-20 04:26:08
【问题描述】:

我在使用 XML 模式的唯一约束时遇到了一点问题。

这是要验证的 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% 确定我将唯一约束放置在正确的位置 (假设根元素和你存在但没有包含在上面以节省一些空间,一切都很好[也许除了唯一约束])

如果有人能引导我朝着正确的方向前进,那就太棒了! (另外,如果您发现还有什么做得不好的地方,请告诉)

【问题讨论】:

    标签: xml validation xsd unique


    【解决方案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" />
    

    看起来命名空间是问题所在。我希望这会有所帮助。

    【讨论】:

    • 唉,我仍然无法得到正确验证的代码。感谢您的帮助,但据我所知,我的代码看起来与您的非常相似,但仍然无法正常工作。 (我也希望属性“POSonBoard”不仅针对典当块进行全局验证,而且我想,要运行我们应该首先学会爬行)
    • 也许您可以发布您的完整代码?此外,如果您愿意稍微更改 POSonBoard 属性的格式,您是否考虑过将类型更改为 xs:ID?您可以使格式类似于 p[1-32] - 这将是全球唯一的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2014-12-03
    • 2014-10-08
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多