XML Schema: how do you extend and add elements "in the middle"?

J

joshua.davies

I'm pretty new to XML Schema, so this may not be possible, but I
thought I'd ask... I've got a set of similar documents that have
common headers & footers (but separate "middle parts") - e.g.:

<response1>
<metadata>
<address>127.0.0.1</address>
<id>abc@123</id>
</metadata>

<response1Results>...</response1Results>

<environment>dev</environment>
</response1>

<response2>
<metadata>
<address>127.0.0.1</address>
<id>abc@123</id>
</metadata>

<response2results>...</response2Results>

<environment>dev</environment>
</response2>

So the <metadata> element is the common header, and the <environment>
element is the common footer (obviously simplified for illustration
purposes).

I'm trying to model this using XML schema; since there's a commonality
here, I figured I should create an abstract <complexType> and extend
it in both places. Here's what I tried to do:

<xs:complexType name="CommonResponse" abstract="true">
<xs:sequence>
<xs:element name="metadata" ... />

<xs:element name="environment" ... />
</xs:complexType>

and then extend it when I define the concrete types, like this:

<xs:element name="response1">
<xs:complexType>
<xs:complexContent>
<xs:extension base="CommonResponse">
<xs:sequence>
<xs:element name="response1Results" ... />
....

Xerces rejects this because it expects <response1Results> to appear
after <environment> (which makes sense...) If I change the
<xs:sequence> to <xs:all>, the schema doesn't even parse; I get:

An 'all' model group must appear in a particle with {min occurs} =
{max occurs} = 1, and that particle must be part of a pair which
constitutes the {content type} of a complex type definition.

which I don't really understand...

Is what I'm trying to model impossible to express this way? It's no
big deal if I have to do a bit of duplication, but I'm curious if this
is supported/the "right" way to do things.
 
P

Pavel Lepin

(e-mail address removed)
I'm pretty new to XML Schema, so this may not be possible,
but I thought I'd ask... I've got a set of similar
documents that have common headers & footers (but separate
"middle parts") - e.g.:

<response1>
<metadata>
<address>127.0.0.1</address>
<id>abc@123</id>
</metadata>

<response1Results>...</response1Results>

<environment>dev</environment>
</response1>

<response2>
<metadata>
<address>127.0.0.1</address>
<id>abc@123</id>
</metadata>

<response2results>...</response2Results>

<environment>dev</environment>
</response2>

So the <metadata> element is the common header, and the
<environment> element is the common footer (obviously
simplified for illustration purposes).

So move the environment element above the variable part.
 
J

joshua.davies

(e-mail address removed)
<[email protected]>:




So move the environment element above the variable part.

Well yeah, I can (or I can just cut and paste the footer definition
into each schema - there's more to it than just that one element), I
just wondered if what I was trying to do was even possible/expressible
in XML schema.

So, it's not?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top