繁体   English   中英

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

[英]No referenced value found for keyRef using XML Schema xs:key and xs:keyref

我正在尝试向我的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>

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

<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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM