Schema - Definition for checking part of schema ?

Discussion in 'XML' started by Abhinav, Aug 16, 2004.

  1. Abhinav

    Abhinav Guest

    Hi,

    I have an xml which contains the following

    <a>
    <!-- aa Is what I am interested in -->
    <aa>
    </aa>

    <bb></bb>
    <cc></cc>

    </a>

    Here, my application requires that <a> (The root element) *must* contain
    <aa> tag.

    However, tags such as <bb>, <cc>, or anything else might exist - they are
    optional, and I do not know which of them (if at all) will be present.

    How do I write the schema for it ?

    Using all requires that I know which tags might appear ..

    Using any doesnt allow me to check the presence of the specific tag <aa>

    Any pointers on how to achieve this ?

    TIA

    --
    Abhinav
     
    Abhinav, Aug 16, 2004
    #1
    1. Advertising

  2. Abhinav wrote:


    > I have an xml which contains the following
    >
    > <a>
    > <!-- aa Is what I am interested in -->
    > <aa>
    > </aa>
    >
    > <bb></bb>
    > <cc></cc>
    >
    > </a>
    >
    > Here, my application requires that <a> (The root element) *must* contain
    > <aa> tag.
    >
    > However, tags such as <bb>, <cc>, or anything else might exist - they
    > are optional, and I do not know which of them (if at all) will be present.
    >
    > How do I write the schema for it ?
    >
    > Using all requires that I know which tags might appear ..
    >
    > Using any doesnt allow me to check the presence of the specific tag <aa>
    >
    > Any pointers on how to achieve this ?


    It is simple, use a sequence with one defined element and xs:any:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0">

    <xs:element name="a">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="aa" type="xs:string" />
    <xs:any processContents="lax" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 16, 2004
    #2
    1. Advertising

  3. Abhinav

    Abhinav Guest

    Martin Honnen wrote:

    >
    > Abhinav wrote:
    >
    >
    >
    >>I have an xml which contains the following
    >>
    >><a>
    >> <!-- aa Is what I am interested in -->
    >> <aa>
    >> </aa>
    >>
    >> <bb></bb>
    >> <cc></cc>
    >>
    >></a>
    >>
    >>Here, my application requires that <a> (The root element) *must* contain
    >><aa> tag.
    >>
    >>However, tags such as <bb>, <cc>, or anything else might exist - they
    >>are optional, and I do not know which of them (if at all) will be present.
    >>
    >>How do I write the schema for it ?
    >>
    >>Using all requires that I know which tags might appear ..
    >>
    >>Using any doesnt allow me to check the presence of the specific tag <aa>
    >>
    >>Any pointers on how to achieve this ?

    >
    >
    > It is simple, use a sequence with one defined element and xs:any:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xs:schema
    > xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > version="1.0">
    >
    > <xs:element name="a">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element name="aa" type="xs:string" />
    > <xs:any processContents="lax" maxOccurs="unbounded" />
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    >
    > </xs:schema>
    >


    Thanks .. That worked ..

    However, If I want to ignore elements both *before* and *after* <aa> ?
    adding another <xs:any> before <xs:element name="aa"> does not work.
    XML::Xerces gives the error :

    MESSAGE: Not enough elements to match content model : '((,aa),)'

    Any pointers appreciated!

    TIA
    --
    Abhinav
     
    Abhinav, Aug 16, 2004
    #3
  4. Abhinav wrote:

    > However, If I want to ignore elements both *before* and *after* <aa> ?
    > adding another <xs:any> before <xs:element name="aa"> does not work.
    > XML::Xerces gives the error :


    Is the number of elements before <aa> known? Otherwise I think you get a
    problem with the schema being non-deterministic.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 16, 2004
    #4
  5. Abhinav

    Abhinav Guest

    Martin Honnen wrote:
    >
    > Abhinav wrote:
    >
    >
    >>However, If I want to ignore elements both *before* and *after* <aa> ?
    >>adding another <xs:any> before <xs:element name="aa"> does not work.
    >>XML::Xerces gives the error :

    >
    >
    > Is the number of elements before <aa> known? Otherwise I think you get a
    > problem with the schema being non-deterministic.
    >

    Hmm ..

    It is not known .. but it is definitely more than 1.

    Putting maxOccurs="10" (An arbotrary value I can live with) did not solve
    the problem ..

    Do you mean to say that it is not possible to do it at all ? (I read the
    Schema specs on w3c.org, but could not find to many pointers.

    TIA

    --

    Abhinav
     
    Abhinav, Aug 16, 2004
    #5
  6. Abhinav wrote:


    >> Is the number of elements before <aa> known? Otherwise I think you get
    >> a problem with the schema being non-deterministic.
    >>


    > It is not known .. but it is definitely more than 1.
    >
    > Putting maxOccurs="10" (An arbotrary value I can live with) did not
    > solve the problem ..
    >
    > Do you mean to say that it is not possible to do it at all ?


    From my current understanding and tests with Xerces-J and MSXML you
    need to specify a fixed number of occurances otherwise the parser is
    unable to determine where the element you want to check for is. But
    maybe someone else comes up with a workaround.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 16, 2004
    #6
  7. Abhinav

    Abhinav Guest

    Martin Honnen wrote:
    >
    > Abhinav wrote:
    >
    >
    >
    >>>Is the number of elements before <aa> known? Otherwise I think you get
    >>>a problem with the schema being non-deterministic.
    >>>

    >
    >
    >>It is not known .. but it is definitely more than 1.
    >>
    >>Putting maxOccurs="10" (An arbotrary value I can live with) did not
    >>solve the problem ..
    >>
    >>Do you mean to say that it is not possible to do it at all ?

    >
    >
    > From my current understanding and tests with Xerces-J and MSXML you
    > need to specify a fixed number of occurances


    Even the fixed number of occurences does not work in Xerces-P ! The only
    thing that works in minOccurs=maxOccurs=1.

    otherwise the parser is
    > unable to determine where the element you want to check for is. But
    > maybe someone else comes up with a workaround.


    Regards
    --
    Abhinav
     
    Abhinav, Aug 16, 2004
    #7
    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,670
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,330
    Stanimir Stamenkov
    Apr 25, 2005
  3. Jianli Shen
    Replies:
    1
    Views:
    645
    Victor Bazarov
    Mar 13, 2005
  4. Ark
    Replies:
    1
    Views:
    452
    Chris Torek
    Aug 7, 2004
  5. Jon Slaughter
    Replies:
    4
    Views:
    473
    Jon Slaughter
    Oct 26, 2005
Loading...

Share This Page