Here's the problem I'm trying to solve. I want one node to be able to refer to multiple other nodes. Like this:
这就是我要解决的问题。我希望一个节点能够引用其他多个节点。是这样的:
<ranges>
<range localid="0001">2013-05-06</range>
<range localid="0010">2014-01-02</range>
<range localid="0100">2014-03-09</range>
<range localid="1000">2014-11-12</range>
</ranges>
<speakers>
<speaker crossrefs="0011">Sagan</speaker>
<speaker crossrefs="1010">Krauss</speaker>
</speakers>
Using a kind of bitfield would allow my "speaker" nodes to refer to multiple date ranges. But "speaker" is not the only node I'd like to cross-reference in this way. I have lots of other nodes and subnodes, and I'd like to avoid adding a "crossrefs" attribute to every node I create in the schema, which is very much in flux right now anyway, as I add new element types. Is there anything in xml that would allow me to declare "crossrefs" as an optional attribute for every node in the document?
使用一种位字段可以让我的“speaker”节点引用多个日期范围。但是“speaker”并不是我想用这种方式进行交叉引用的唯一节点。我有很多其他的节点和子节点,我希望避免在模式中创建的每个节点上添加“crossrefs”属性,无论如何,这在我添加新元素类型时非常不稳定。xml中有什么东西可以让我将“crossrefs”声明为文档中每个节点的可选属性吗?
It occurrs to me that this would be just like the "id" attribute that xml allows on every node. I looked at the schema for xml schema, to see how the "id" attribute is defined, but I can't see anything there that looks like the thing I'm looking for. Is there a way to do this?
对我来说,这就像xml允许在每个节点上使用的“id”属性一样。我查看了xml模式的模式,以查看如何定义“id”属性,但是我在那里看不到任何类似于我正在查找的东西。有办法吗?
I had already looked into xml cross-references. The problem with that, at least as far as I understand cross-references, is that I'd have to add a subnode to every node, to have another node that acts as a cross reference. Ugly. Is there a less ugly way to do this sort of thing?
我已经研究过xml交叉引用。这样做的问题是,至少就我所理解的交叉引用而言,我必须向每个节点添加一个子节点,才能有另一个节点充当交叉引用。丑。有什么比这更丑陋的方法来做这种事吗?
Edit:
编辑:
It seems that this might be the same as the question I'm asking, but it's so far over my head that I can't tell whether it really is the same.
这似乎和我问的问题是一样的,但是我的头脑是如此之远以至于我无法判断它是否真的是一样的。
2 个解决方案
#1
2
Define an abstract base type that allows the crossrefs attribute, and then derive all the other element types from that one by extension.
定义一个抽象基类型,它允许交叉refs属性,然后通过扩展派生所有其他元素类型。
#2
4
In XSD 1.1, define an attribute group with the attributes you wish to be global. Then supply the defaultAttributes
attribute on the xsd:schema
element, with the name of the attribute group as its value.
在XSD 1.1中,定义一个属性组,该属性组具有您希望的全局属性。然后在xsd:schema元素上提供defaultAttributes属性,其值为属性组的名称。
Let's assume you name this attribute group my-globals
. Your schema document will look (in part) like this:
让我们假设您将这个属性组命名为my-globals。您的模式文档(部分)将如下所示:
<xsd:schema targetNamespace="http://example.com/global-atts"
xmlns:tns="http://example.com/global-atts"
defaultAttributes="tns:my-globals"
...>
<xsd:attributeGroup name="my-globals">
<xsd:attribute name="crossrefs" ... />
...
</
...
</
In XSD 1.0, do as Michael Kay says. If that's not feasible, at least declare the named attribute group and refer to it from all the complex types that should have it.
在XSD 1.0中,按照迈克尔·凯的话去做。如果不可行,至少声明命名属性组,并从应该具有它的所有复杂类型中引用它。
#1
2
Define an abstract base type that allows the crossrefs attribute, and then derive all the other element types from that one by extension.
定义一个抽象基类型,它允许交叉refs属性,然后通过扩展派生所有其他元素类型。
#2
4
In XSD 1.1, define an attribute group with the attributes you wish to be global. Then supply the defaultAttributes
attribute on the xsd:schema
element, with the name of the attribute group as its value.
在XSD 1.1中,定义一个属性组,该属性组具有您希望的全局属性。然后在xsd:schema元素上提供defaultAttributes属性,其值为属性组的名称。
Let's assume you name this attribute group my-globals
. Your schema document will look (in part) like this:
让我们假设您将这个属性组命名为my-globals。您的模式文档(部分)将如下所示:
<xsd:schema targetNamespace="http://example.com/global-atts"
xmlns:tns="http://example.com/global-atts"
defaultAttributes="tns:my-globals"
...>
<xsd:attributeGroup name="my-globals">
<xsd:attribute name="crossrefs" ... />
...
</
...
</
In XSD 1.0, do as Michael Kay says. If that's not feasible, at least declare the named attribute group and refer to it from all the complex types that should have it.
在XSD 1.0中,按照迈克尔·凯的话去做。如果不可行,至少声明命名属性组,并从应该具有它的所有复杂类型中引用它。