2

我有一些 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:unique 的 xs:element 的子元素。

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

Is there a solution I have missed (still using XSDs)? Thanks in advance..

4

1 回答 1

3

The XSD below would be my solution to your problem. I basically fixed some of the selectors and put them at the same level.

QTAssistant showing unique/keyref

The 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> 
于 2012-08-03T18:08:25.337 回答