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

Discussion in 'XML' started by joshua.davies@travelocity.com, Dec 11, 2007.

  1. Guest

    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.
     
    , Dec 11, 2007
    #1
    1. Advertising

  2. Pavel Lepin Guest


    <> wrote in
    <>:
    > 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.

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Dec 11, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 12, 6:16 am, Pavel Lepin <> wrote:
    >
    > <> wrote in
    > <>:
    >
    >
    >
    > > I'm pretty new to XML Schema, so this may not be possible,

    >
    > 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?
     
    , Dec 11, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page