tags:

views:

46

answers:

1

Is it possible (and if so, how) to enforce with XML Schema that two elements in a document must contain an identical substructure? For instance, I'd like to express that any foo has two children, bar1 and bar2, and bar1 has to have the same child structure as bar2:

<foo>
  <bar1>
    <baz>hello, world</baz>
  </bar1>
  <bar2>
    <baz>hello, world</baz>
  </bar2>
</foo>

Is key and keyref the right way to go?

Thanks!

A: 

Sure - define a named <xs:complexType> which represents the "contents" of the bar1 and bar2 nodes, and use that to define the two elements:

<xs:schema id="foo" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="foo">
    <xs:complexType>
      <xs:element name="bar1" type="subbar" />
      <xs:element name="bar2" type="subbar" />
    </xs:complexType>
  </xs:element>
  <xs:complexType name="subbar">
     <xs:sequence>
        <xs:element name="baz" type="xs:string" minOccurs="0" />
     </xs:sequence>
  </xs:complexType>
</xs:schema>
marc_s
Thanks for the answer - but apparently I wasn't clear enough with my question. The problem is that I want to make sure that the child content is *exactly* the same for bar1,2 - e.g., if bar1/baz has the string content "hello, world" then bar2/baz must also have "hello, world" as content.
Thomas