XML,XSD xs:递归复杂类型定义中的keyref

时间:2012-08-03 15:38:17

标签: xml recursion xsd xerces keyref

我在顶部有一些带有允许类型列表的xml数据,以及一个引用允许类型的对象和子对象的递归列表类型:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KEYREF_TEST.xsd">
  <ALLOWED_SHAPE_TYPES>
    <SHAPE_TYPE>Circle</SHAPE_TYPE>
    <SHAPE_TYPE>Triangle</SHAPE_TYPE>
    <SHAPE_TYPE>Square</SHAPE_TYPE>
  </ALLOWED_SHAPE_TYPES>
  <SHAPES>
    <SHAPE>
      <TYPE>Triangle</TYPE>
      <SUB_SHAPES>
        <SHAPE>
          <TYPE>Circle</TYPE>
          <SUB_SHAPES/>
        </SHAPE>
        <SHAPE>
          <TYPE>Square</TYPE>
          <SUB_SHAPES>
            <SHAPE>
              <TYPE>Triangle</TYPE>
              <SUB_SHAPES/>
            </SHAPE>
          </SUB_SHAPES>
        </SHAPE>
      </SUB_SHAPES>
    </SHAPE>
    <SHAPE>
      <TYPE>Square</TYPE>
      <SUB_SHAPES/>
    </SHAPE>
  </SHAPES>
</ROOT>

我已经定义了验证此文档的模式,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ROOT">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ALLOWED_SHAPE_TYPES">
          <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
              <xs:element name="SHAPE_TYPE" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="SHAPES" type="shape-list-type"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="SHAPE_TYPE_UK">
      <xs:selector xpath="ALLOWED_SHAPE_TYPES/SHAPE_TYPE"/>
      <xs:field xpath="."/>
    </xs:unique>
  </xs:element>
  <xs:complexType name="shape-list-type">
    <xs:sequence>
      <xs:element name="SHAPE" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
         <xs:sequence>
            <xs:element name="TYPE" type="xs:string">
              <xs:keyref name="SHAPE_TYPE_FK" refer="SHAPE_TYPE_UK">
                <xs:selector xpath="."></xs:selector>
                <xs:field xpath="."></xs:field>
              </xs:keyref>
            </xs:element>
            <xs:element name="SUB_SHAPES" type="shape-list-type"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

在oXygen 13.2中验证时,我收到以下错误:

System ID: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
Main validation file: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
Schema: C:\pvcswork\CodeSource\XMLSchema\KEYREF_TEST.xsd
Engine name: Xerces
Severity: error
Description: Identity Constraint error:  identity constraint "KeyRef@1df2ead" has a keyref which refers to a key or unique that is out of scope.
Start location: 10:28

我认为这是因为xs:keyref是xs:complexType定义的子代,它不是xs:element的子元素,其中定义了xs:unique。

我可以将xs:keyref移动到ROOT定义中并使用XPath定位TYPE元素,但不允许使用//语法,因此我将丢失递归并且必须为每个级别定义一个新的xs:keyref筑巢。 ** UPDATE - //在XPath开始时被允许,前面有一个点! **

我有错过的解决方案(仍在使用XSD)吗?提前谢谢..

1 个答案:

答案 0 :(得分:3)

下面的XSD将是我解决问题的方法。我基本上修复了一些选择器并将它们放在同一级别。

QTAssistant showing unique/keyref

XSD:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="ROOT"> 
        <xs:complexType> 
            <xs:sequence> 
                <xs:element name="ALLOWED_SHAPE_TYPES"> 
                    <xs:complexType> 
                        <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
                            <xs:element name="SHAPE_TYPE" type="xs:string"/> 
                        </xs:sequence> 
                    </xs:complexType> 
                </xs:element> 
                <xs:element name="SHAPES" type="shape-list-type"/> 
            </xs:sequence> 
        </xs:complexType> 
        <xs:unique name="SHAPE_TYPE_UK"> 
            <xs:selector xpath="ALLOWED_SHAPE_TYPES/SHAPE_TYPE"/> 
            <xs:field xpath="."/> 
        </xs:unique> 
        <xs:keyref name="SHAPE_TYPE_FK" refer="SHAPE_TYPE_UK"> 
            <xs:selector xpath=".//SHAPE/TYPE"></xs:selector> 
            <xs:field xpath="."></xs:field> 
        </xs:keyref>        
    </xs:element> 
    <xs:complexType name="shape-list-type"> 
        <xs:sequence> 
            <xs:element name="SHAPE" minOccurs="0" maxOccurs="unbounded"> 
                <xs:complexType> 
                    <xs:sequence> 
                        <xs:element name="TYPE" type="xs:string"/> 
                        <xs:element name="SUB_SHAPES" type="shape-list-type"/> 
                    </xs:sequence> 
                </xs:complexType> 
            </xs:element> 
        </xs:sequence> 
    </xs:complexType> 
</xs:schema>