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

查看:13
本文介绍了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 book,它似乎教了关于 key/keyref 的正确定义以及如何它类似于 ID/IDREF(但更好),并没有给出一个简单的使用示例.它似乎并不相似,因为您将 ID 定义为一个元素中的属性,而将 IDREF 定义为另一个元素中的属性,瞧.但是 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 文件通过 XJC 生成 JAXB 绑定的 Java 类

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

我已经搜索了操作方法、教程和示例,但谷歌给了我一些残羹剩饭.对于 SO 上的搜索(也使用 google 和使用 '+' 的包容性搜索).

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,其中已经定义了我所理解的 key/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 提出的更改,将关键元素的 xpath 属性更改为所有者".但 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.

谢谢

推荐答案

我发现这个线程正在搜索 OP 正在搜索的相同内容 - <xs:key> 的简单用法示例 元素.所有 JAXB 的东西对我来说都是希腊式的,而且会分散注意力.对于后来发现这个线程的其他人,这是在 OP 在 SO 上提出问题几年后发布在 MSDN 上的一个简单示例:

I found this thread searching for the same thing the OP was searching for - a simple usage example of the <xs:key> element. All the JAXB stuff was greek to me, and a distraction. For others finding this thread later, here's a simple example posted on MSDN a couple years after the OP asked the question here on SO:

https://msdn.microsoft.com/en-us/library/ms256101%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

如果 MSDN 链接发生更改,则面包屑路径为:

In case the MSDN link changes, the breadcrumb path was:

https://msdn.microsoft.com/library 然后点击切换到库目录视图",并向下钻取:

https://msdn.microsoft.com/library then click "Switch to Library TOC view", and drill down through:

MSDN 库 > .NET 开发 > .NET Framework 4.6 和 4.5 > 开发指南 > 数据和建模 > XML 标准参考 > XML 架构 (XSD) 参考 > XML 架构元素 > <xsd:key> 元素

MSDN Library > .NET development > .NET Framework 4.6 and 4.5 > Development Guide > Data and Modeling > XML Standards Reference > XML Schemas (XSD) Reference > XML Schema Elements > <xsd:key> Element

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

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