Strange XML nesting and schema validation

Discussion in 'XML' started by Arthur, Feb 3, 2005.

  1. Arthur

    Arthur Guest

    I've come across some strange xml, that I need to deal with, it looks
    like this:-

    <root>
    <foo attr="1">Some random strange text.
    <bar attr="2">blar</bar>
    <bar attr="3">blar blar</bar>
    <bar attr="4">blar blar blar</bar>
    </foo>
    </root>

    Its the 'Some random strange text' in the between the foo tags along
    with all the bar tags.
    First of all, if the above actually legal well formed XML?

    And if so, what would some schema look like to validate it?

    I've tried something similar to this:-

    ....
    <xs:element name="foo">
    <xs:complexType>
    <xs:simpleContent>
    <xs:extension base="xs:string">
    <xs:attribute name="attr" type="xs:int" use="required"/>
    </xs:extension>
    </xs:simpleContent>
    <xs:choice>
    <xs:element name="bar" minOccurs="0">
    <xs:complexType>
    <xs:attribute name="attr" type="xs:int" use="required"/>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    ....

    Buts this doesn't work. :(

    Anyone got any ideas?

    Arthur.
     
    Arthur, Feb 3, 2005
    #1
    1. Advertising

  2. Hi Arthur,

    It's allowed - it's known as mixed content. I wouldn't recommend it for
    anything other than free-form text (like books, HTML, etc.) but if you
    have no control over it...

    Here is a type that will validate it:

    <xs:element name="foo">
    <xs:complexType mixed="true">
    <xs:sequence>
    <xs:element name="bar" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:simpleContent>
    <xs:extension base="xs:string">
    <xs:attribute name="attr" type="xs:int" use="required"/>
    </xs:extension>
    </xs:simpleContent>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="attr" type="xs:int" use="required"/>
    </xs:complexType>
    </xs:element>

    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 3, 2005
    #2
    1. Advertising

  3. In article <lhpMd.228$>,
    Arthur <> wrote:

    >Its the 'Some random strange text' in the between the foo tags along
    >with all the bar tags.
    >First of all, if the above actually legal well formed XML?


    Consider

    <p>This is <b>bold</b><p>

    This "mixed context" is perfectly normal when XML is used for marking
    up text - which was the original purpose of SGML and XML - but is not
    usually a good idea when it's used for representing data structures.

    >And if so, what would some schema look like to validate it?


    See http://www.w3.org/TR/xmlschema-0/#mixedContent

    -- Richard
     
    Richard Tobin, Feb 3, 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. Markus
    Replies:
    1
    Views:
    1,550
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,268
    Stanimir Stamenkov
    Apr 25, 2005
  3. Replies:
    3
    Views:
    2,874
  4. Trans
    Replies:
    10
    Views:
    306
    Sean O'Halpin
    Sep 16, 2005
  5. Replies:
    5
    Views:
    1,029
    Brian McCauley
    Nov 29, 2006
Loading...

Share This Page