XML Schemas Question

Discussion in 'XML' started by abu123, Feb 21, 2005.

  1. abu123

    abu123 Guest

    Hi,

    I am still a newbie with XML and was hoping someone could help me with
    this query. I want to create an XML Schema that would allow me to have
    two elements that have a different structure in two different areas of
    XML. It's probably easier if I provide a sample XML :-

    <MyXML>
    <MDP type="MDP">
    <linkFactor type="linkFactor">
    <value type="decimal">0.9</value>
    </linkFactor>
    </MDP>
    <MDS type="MDS">
    <linkFactor type="linkFactor">0.90</linkFactor>
    </MDS>
    </MyXML>

    Basically, in the above sample linkfactor is present twice, but in the
    MDP section I want it to have a value tag.

    Is it possible to create an XML Schema to correspond to this ?
    I was thinking I need to hide the definitions of linkfactor so that I
    have two one for each element. My thoughts of what the actual schema
    defn. of this type would be

    <xs:complexType name="linkFactorType" mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="value" type="valueType"/>
    </xs:choice>
    <xs:attribute name="type" type="xs:string" use="required"/>
    </xs:complexType>

    for the expanded version that includes the value tag and

    <xs:element name="linkFactor" type="linkFactorType"/>

    for the simpler type. Just confused on how to pick-up one defn over the
    other.


    Any guidance greatly appreciated.

    Many Thanks,
    Ab
     
    abu123, Feb 21, 2005
    #1
    1. Advertising

  2. Hi Ab,

    You would need a separate complex type definition for the "simpler"
    linkFactor, something like this:

    <xs:complexType name="linkFactorType2">
    <xs:simpleContent>
    <xs:extension base="xs:decimal">
    <xs:attribute name="type" type="xs:string" use="required"/>
    </xs:extension>
    </xs:simpleContent>
    </xs:complexType>

    Then, you would declare the two linkFactor elements locally with
    different types, depending on which parent definition they are in. For
    example:

    <xs:element name="MDP" type="MDPType"/>
    <xs:complexType name="MDPType">
    <xs:sequence maxOccurs="unbounded">
    <xs:element name="linkFactor" type="linkFactorType"/>
    </xs:sequence>
    <xs:attribute name="type" type="xs:string" use="required"/>
    </xs:complexType>

    and

    <xs:element name="MDS" type="MDSType"/>
    <xs:complexType name="MDSType">
    <xs:sequence maxOccurs="unbounded">
    <xs:element name="linkFactor" type="linkFactorType2"/>
    </xs:sequence>
    <xs:attribute name="type" type="xs:string" use="required"/>
    </xs:complexType>


    Hope that helps,
    Priscilla
    ----------------------------------
    Priscilla Walmsley
    Author, Definitive XML Schema
    http://www.datypic.com
    ----------------------------------

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Priscilla Walmsley, Feb 21, 2005
    #2
    1. Advertising

  3. abu123

    abu123 Guest

    Thanks Priscilla. This exactly does what I'm after.

    Cheers.
    Abu
     
    abu123, Feb 21, 2005
    #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.
Similar Threads
  1. Soeren
    Replies:
    2
    Views:
    358
    Waxolunist
    Sep 22, 2004
  2. Replies:
    3
    Views:
    791
  3. Tony Prichard
    Replies:
    0
    Views:
    731
    Tony Prichard
    Dec 12, 2003
  4. Sean Bright
    Replies:
    2
    Views:
    442
    Sean Bright
    Dec 17, 2003
  5. Andy B
    Replies:
    0
    Views:
    379
    Andy B
    Aug 7, 2008
Loading...

Share This Page