XSD:密钥和密钥引用验证 [英] XSD: key and keyref validation

查看:36
本文介绍了XSD:密钥和密钥引用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模式验证方面遇到问题,这是我的 xml

<收藏夹><文件夹><姓名>娱乐</姓名><书签><书签 ID="1"/><书签 ID="200"/><!-- 这应该会失败验证--></书签></文件夹></收藏夹><书签><书签><ID>1</ID><URL>www.website1.com</URL></书签><书签><ID>2</ID><URL>www.website2.com</URL></书签><书签><ID>3</ID><URL>www.website3.com</URL></书签></书签></root>

Bookmark 中的 ID 元素应该是键,并且收藏夹/文件夹/书签/书签 ID 属性应该总是引用一个书签 ID.

这是我的 xsd:

<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name="root"><xs:complexType><xs:序列><xs:element name="Favorites" type="Favorites"/><xs:element name="Bookmarks" type="Bookmarks"/></xs:sequence></xs:complexType><xs:keyref name="bookmarkIDKeyRef"refer="bookmarkIDKey"><xs:selector xpath="root/Favorites/Folder/Bookmarks/*"/><xs:field xpath="@ID"/></xs:keyref><xs:key name="bookmarkIDKey"><xs:selector xpath="root/Bookmarks/Bookmark/ID"/><xs:field xpath="ID"/></xs:key></xs:element><xs:complexType name="收藏夹"><xs:sequence maxOccurs="unbounded"><xs:element name="文件夹" type="文件夹"/></xs:sequence></xs:complexType><xs:complexType name="文件夹"><xs:序列><xs:element name="Name" type="xs:string"/><xs:element name="书签"><xs:complexType><xs:sequence maxOccurs="unbounded"><xs:element name="书签"><xs:complexType><xs:attribute name="ID" type="xs:string"/></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType><xs:complexType name="书签"><xs:sequence maxOccurs ="无界"><xs:element name="Bookmark" type="Bookmark"/></xs:sequence></xs:complexType><xs:complexType name="书签"><xs:序列><xs:element name="ID" type="xs:string"/><xs:element name="URL" type="xs:string"/></xs:sequence></xs:complexType></xs:schema>

xml 格式正确,但对 ID 属性的验证不起作用.

解决方案

键的上下文是 元素,因为在架构中它们是 < 的子元素;xs:element name="root"> 元素.因此,您的 XPath 失败,因为它们以 root/... 开头,它不是 的子级代码>元素.为 keykeyref 删除它.由于类似的问题,密钥的 也失败了.您已经选择 元素作为上下文,然后尝试为该元素查找 子元素.将 的 XPath 切换为 .(一个点)或从选择器中删除尾随的 /ID.>

这是一个有效的代码示例.

<xs:complexType><xs:序列><xs:element name="Favorites" type="Favorites"/><xs:element name="Bookmarks" type="Bookmarks"/></xs:sequence></xs:complexType><xs:keyref name="bookmarkIDKeyRef"refer="bookmarkIDKey"><xs:selector xpath="收藏夹/文件夹/书签/*"/><xs:field xpath="@ID"/></xs:keyref><xs:key name="bookmarkIDKey"><xs:selector xpath="书签/书签"/><xs:field xpath="ID"/></xs:key></xs:element>

I'm having issues with schema validation, this is my xml

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation='favorites.xsd'>
    <Favorites>
        <Folder>
            <Name>Entertainment</Name>
            <Bookmarks>
                <Bookmark ID="1"/>
                <Bookmark ID="200"/> <!-- This should fail validation -->
            </Bookmarks>
        </Folder>
    </Favorites>
    <Bookmarks>
        <Bookmark>
            <ID>1</ID>
            <URL>www.website1.com</URL>
        </Bookmark>
        <Bookmark>
            <ID>2</ID>
            <URL>www.website2.com</URL>
        </Bookmark>
        <Bookmark>
            <ID>3</ID>
            <URL>www.website3.com</URL>
        </Bookmark>
    </Bookmarks>
</root>

The ID element in Bookmark should be the key, and the Favorites/Folder/Bookmarks/Bookmark ID attribute should always reference a Bookmark ID.

Here is my xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Favorites" type="Favorites"/>
                <xs:element name="Bookmarks" type="Bookmarks"/>
            </xs:sequence>
        </xs:complexType>
        <xs:keyref name="bookmarkIDKeyRef" refer="bookmarkIDKey">
            <xs:selector xpath="root/Favorites/Folder/Bookmarks/*"/>
            <xs:field xpath="@ID"/>
        </xs:keyref>
        <xs:key name="bookmarkIDKey">
            <xs:selector xpath="root/Bookmarks/Bookmark/ID"/>
            <xs:field xpath="ID"/>
        </xs:key>
    </xs:element>

    <xs:complexType name="Favorites">
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="Folder" type="Folder"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Folder">
        <xs:sequence>
            <xs:element name="Name" type="xs:string"/>
            <xs:element name="Bookmarks">
                <xs:complexType>
                    <xs:sequence maxOccurs="unbounded">
                        <xs:element name="Bookmark">
                            <xs:complexType>
                                <xs:attribute name="ID" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Bookmarks">
        <xs:sequence maxOccurs ="unbounded">
            <xs:element name="Bookmark" type="Bookmark"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Bookmark">
        <xs:sequence>
            <xs:element name="ID" type="xs:string"/>
            <xs:element name="URL" type ="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

The xml is well formed, but the validation on the ID attribute is not working.

解决方案

The context of your keys is the <root> element, because in the schema they are children of the <xs:element name="root"> element. Therefore your <xs:selector> XPaths fail, because they begin with root/... which is not a child of the <root> element. Drop it for both key and keyref. Also the <xs:field> of the key fails due to similar issue. You already select the <ID> element as the context and then try to look for an <ID> child for this element. Switch the XPath of the <xs:field> to . (a dot) or drop the trailing /ID from the selector.

Here is a working code sample.

<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Favorites" type="Favorites"/>
            <xs:element name="Bookmarks" type="Bookmarks"/>
        </xs:sequence>
    </xs:complexType>
    <xs:keyref name="bookmarkIDKeyRef" refer="bookmarkIDKey">
        <xs:selector xpath="Favorites/Folder/Bookmarks/*"/>
        <xs:field xpath="@ID"/>
    </xs:keyref>
    <xs:key name="bookmarkIDKey">
        <xs:selector xpath="Bookmarks/Bookmark"/>
        <xs:field xpath="ID"/>
    </xs:key>
</xs:element>

这篇关于XSD:密钥和密钥引用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆