使用XML模式xs:key和xs:keyref找不到keyRef的引用值

时间:2018-11-04 19:44:47

标签: xml xsd foreign-keys constraints

我正在尝试将约束添加到我的XML Schema模式中,但是我正在监视一个错误。我不断得到:

  1. 找不到keyRef {author}的引用值:2
  2. 未找到keyRef {author}的引用值:3

我不知道我在做什么错。我的keyref显然工作正常,但key无效。我该如何解决?

这是架构:

    <?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="articles">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="article" type="article" maxOccurs="unbounded">
                                    <xs:keyref name="author" refer="authorKey">
                                        <xs:selector xpath="author"/>
                                        <xs:field xpath="@ref-number"/>
                                    </xs:keyref>
                                    <xs:unique name="unique-author">
                                        <xs:selector xpath="author"/>
                                        <xs:field xpath="@ref-number"/>
                                    </xs:unique>
                                  </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="authors" type="authors" />
                </xs:sequence>  
            </xs:complexType>   
        </xs:element>

        <xs:complexType name="article">
            <xs:sequence>
                <xs:element name="author" maxOccurs="unbounded" minOccurs="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="ref-number" type="xs:integer" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>     
            </xs:sequence>
        </xs:complexType>

        <xs:complexType name="authors">
            <xs:sequence>
                <xs:element name="author" type="author">
                    <xs:key name="authorKey">
                        <xs:selector xpath="."/>
                        <xs:field xpath="@key-number"/>
                    </xs:key>
                </xs:element>        
            </xs:sequence>    
        </xs:complexType>

         <xs:complexType name="author"> 
             <xs:attribute name="key-number" type="xs:integer" use="required" />
         </xs:complexType> 

    </xs:schema>

这是无法验证的示例XML文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="m5-schema.xsd">
        <articles>
            <article>
                <author ref-number="2"></author>
                <author ref-number="3"></author>     
            </article>
        </articles>
        <authors>
            <author key-number="2">
            </author>
        </authors>
    </root>

1 个答案:

答案 0 :(得分:0)

这似乎是一个范围问题,因此将关键约束移至根并为根定义命名类型会有所帮助:

<xs:element name="root" type="root">  
    <xs:keyref name="author" refer="authorKey">
        <xs:selector xpath=".//article/author"/>
        <xs:field xpath="@ref-number"/>
    </xs:keyref>
    <xs:unique name="unique-author">
        <xs:selector xpath=".//authors/author"/>
        <xs:field xpath="@ref-number"/>
    </xs:unique>
</xs:element>

<xs:complexType name="root">
    <xs:sequence>
        <xs:element name="articles">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="article" type="article" maxOccurs="unbounded">
                      </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="authors" type="authors" />
    </xs:sequence>  
</xs:complexType>