1

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?

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?

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?

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.

Community
  • 1
  • 1
SaganRitual
  • 3,143
  • 2
  • 24
  • 40

2 Answers2

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.

Let's assume you name this attribute group my-globals. Your schema document will look (in part) like this:

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

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • I'm afraid my ambition is outstripping my skills relative to xml. I created a schema per your example. It validates just fine. When I include it into my primary schema, it still validates. But I don't know how to use the attribute. When I add an attribute called `crossrefs` or `tns:crossrefs` to any of my nodes, the validator gives me grief. Could you tell me how to do this, or point me to a resource? Thanks so much. Michael's answer and yours are both over my head, but yours gives me just enough usable detail to encourage me to keep trying. I just don't know where to look for the info I need. – SaganRitual Dec 24 '14 at 03:53
2

Define an abstract base type that allows the crossrefs attribute, and then derive all the other element types from that one by extension.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • It took me a long time to figure out how to do it, but I did it. Thanks for giving me the vocabulary I needed to formulate the right question. – SaganRitual Dec 29 '14 at 21:12
  • Sorry my answer was so brief. It was Dec 23rd... And anyway, I like to point people in the right direction rather than holding their hands and leading them to the destination. – Michael Kay Dec 30 '14 at 00:32