views:

18

answers:

1

Alright, so I'm using XML schema to validate a following type of xml file: (just pretend the parens are actually angled brackets).

<root>
   <A>
      <B></B>
      <C></C>
   </A>
</root>

pretty simple -> except the catch is that I also want to have a comment element, which can occur an unbounded number of times in any order (provided that it isn't a comment within another comment). So the following code sample would also be valid:

<root>
   <comment />
   <A>
      <comment />
      <B>
         <comment />
      </B>
      <comment />
      <C></C>
      <comment />
   </A>
      <comment />
</root>

Initially I had a rigid structure imposed on the initial tree - ie B and C have to appear once, and B must come first. None of the ways I can come up of doing this scale to work on more complex examples. any ideas?

Many thanks

A: 

You could use extensions, as in the following example, but that would only let you add comments at the beginning of each element. If you want comments everywhere, I don't see any way to preserve the sequence order without referencing comments in every sequence in the schema...

<xs:element name="comment" type="xs:string"/>
<xs:complexType name="commentable">
    <xs:sequence>
        <xs:element maxOccurs="unbounded" minOccurs="0" ref="comment"/>
    </xs:sequence>
</xs:complexType>
<xs:element name="root">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="commentable">
                <xs:sequence>
                    <xs:element ref="A"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
<xs:element name="A">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="commentable">
                <xs:sequence>
                    <xs:element ref="B"/>
                    <xs:element ref="C"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
<xs:element name="B">
    <xs:complexType mixed="true">
        <xs:complexContent>
            <xs:extension base="commentable"/>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
<xs:element name="C">
    <xs:complexType mixed="true">
        <xs:complexContent>
            <xs:extension base="commentable"/>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
Damien