XML Schema key / keyref - 如何使用它们? [英] XML Schema key/keyref - how to use them?

查看:179
本文介绍了XML Schema key / keyref - 如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说:我想知道如何使用XSD中的key / keyref让元素互相引用。它必须有一个示例的形式,使用简单的xsd和XML。

Long story short : i would like to know how to use the key/keyref from XSD to let elements have references to each other. it has to have a form of an example, using a simple xsd and an XML.

长篇小说:我熟悉ID / IDREF的用法。我用它来连接JAXB的元素。我一再被告知,XSD中的key / keyref构造为元素间引用提供了增强的灵活性。我查阅了 OReilly XML Schema书,它似乎教授了有关key / keyref正确定义以及如何正确定义的一切内容。它类似于ID / IDREF(但更好)并没有给出一个简单的使用示例。它似乎并不相似,因为您将ID定义为一个元素中的属性,将IDREF定义为另一个元素中的ID。但是key / keyref必须在引用的共同祖先和引用的元素(AFAIK)中定义...

Long story : I am familiar with usage of ID/IDREF. I use those to connect elements for JAXB. I have been told repeatedly that the key/keyref construct in XSD offers enhanced flexibility for inter-element referencing. I have consulted the OReilly XML Schema book, that seems to teach everything about correct definition of key/keyref and how it is similar to the ID/IDREF (but better) and doesn't give a simple example of its use. It doesn't seem to be similar, because you define the ID as an attribute in one element and the IDREF in another element and voila. But key/keyref have to be defined in a common ancestor of the referencing and the referenced element (AFAIK)...

我使用XSD文件生成JAXB绑定使用XJC的Java类

I use the XSD files to generate JAXB-bound Java classes with XJC

我搜索过how-tos,教程和示例,但谷歌给了我一些废话。同样适用于搜索SO(也使用谷歌和包含搜索'+')。

I have searched for how-tos, tutorials and examples, but google gives me scraps. same for searches on SO (also with google and inclusive search with '+' ).

为了让每个人的生活更轻松,我准备了一个已定义密钥的XSD / keyref对,因为我已经明白了。

In order to make everyone's lives easier i have prepared an XSD with already defined key/keyref pair as i have understood it.

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="referenced">
                <xs:complexType>
                    <xs:attribute name="id" type="xs:string" />
                </xs:complexType>
            </xs:element>
            <xs:element name="owner">
                <xs:complexType>
                    <xs:attribute name="id" type="xs:string" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="aKey">
        <xs:selector xpath="owner" />
        <xs:field xpath="@id" />
    </xs:key>
    <xs:keyref name="aKeyRef" refer="aKey">
        <xs:selector xpath="referenced" />
        <xs:field xpath="@id" />
    </xs:keyref>
</xs:element>

一段XML怎么样?看起来像是'所有者'元素引用'引用'元素?

How would a piece of XML look like, with an 'owner'-element referencing a 'referenced'-element?

编辑:应用了Tom W提出的更改,将key元素的xpath属性更改为owner。 JAXB(XJC)仍然不在乎。

EDIT : applied the change proposed by Tom W, changing the xpath attribute of the key element to "owner". JAXB (XJC) still doesnt care though.

谢谢

推荐答案

实例文档中没有特殊语法。简单地要求keyref节点匹配现存的密钥节点。验证将告诉您是否满足关键约束。

There is no special syntax in the instance document. It is simply required that the keyref node matches an extant key node. Validation will tell you whether or not the key constraint is satisfied.

RE您的代码:

我是我自己刚开始涉足钥匙,但我想我发现你的错误 - aKey 应该是这样的:

I've only just started dabbling with keys myself, but I think I've spotted an error in yours - aKey should look like:

<xs:key name="aKey">
    <xs:selector xpath="owner" />
    <xs:field xpath="@id" />
</xs:key>

此外 - 这是一个问题 - 关键限制无法识别默认命名空间。您必须始终使用您正在查找的元素的名称空间前缀为选择器 xpath的每个部分添加前缀。如果你没有命名空间前缀 - 很难,你需要添加一个。这是标准的限制。

Furthermore - this is a gotcha - key constraints don't recognise the default namespace. You must always prefix every part of the selector xpath with the namespace prefix of the element you're looking up. If you don't have a namespace prefix - tough, you'll need to add one. This is a limitation of the standard.

这篇关于XML Schema key / keyref - 如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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