[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1221
  • Last Modified:

XSD - list of complex elements (same name, different types)

Hello,

in an given XML file, there I found such an declaration:

...
<page>
...
  <module class="mod_text">
    <text>...</text>
  </module>
  <module class="mod_list">
    <list>...</list>
  </module>
  <module class="mod_complex">
    <tag1>
      <tag2>...</tag2>
    </tag1>
  </module>
  <module class="mod_text">
    <text>...</text>
  </module>
...
</page>
<page>
...more modules...
</page>

The content of the modules can be disparate. Each module type can occur at several positions per page, also more than one time at a page.

I tried it with these XSD, but it doesn't work:

<xsd:element name="page" minOccurs="0" maxOccurs="unbounded">
      <xsd:complexType>
            <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                  <xsd:choice>
                        <xsd:element name="module" type="mod_text" minOccurs="0"/>
                        <xsd:element name="module" type="mod_list" minOccurs="0"/>
                        <xsd:element name="module" type="mod_complex" minOccurs="0"/>
                  </xsd:choice>
            </xsd:sequence>
      </xsd:complexType>
</xsd:element>

And the module definitions:

<xsd:complexType name="mod_text">
      <xsd:sequence>
            <xsd:element name="text" type"xsd:string" />
      </xsd:sequence>
      <xsd:attribute name="class" fixed="mod_text" use="required"/>
</xsd:complexType>
and so on...

Any idea how to define the XSD for this XML content?
0
pman
Asked:
pman
1 Solution
 
Geert BormansCommented:
you can't!!!

XSD does not allow you to have elements with different types but the same name in the same scope
(at least not schema1.0, that will change with assertions in schema 1.1)

The only thing you can do is make the model for the module element very broad
and use schematron to build the restirctions based on the value of the class attribute
www.Schematron.com
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now