XSD how to ensure the presence of one element ?

Hi,

Let's say that I have a simple xml file with 3 elements <a/>, <b/> and <c/>.
The xml file is valid for my application if it has the <b/> element. No matters if <a/> is there or not. No matters if <c/> is there or not. The problem is that I also have to allow any element in place of <a/> and <c/>.
I could receive an xml file with the 3 following elements <aa/><b/><c/> and it would be valid for me.
How do I have to write my .xsd

Thanks.

a valid xml sample file
< xml version="1.0" encoding="utf-8" >
<result>
<a></a>
<b></b>
<c></c>
</result>

another valid xml sample file
< xml version="1.0" encoding="utf-8" >
<result>
<aa></a>
<b></b>
<c></c>
</result>

an invalid xml sample file
< xml version="1.0" encoding="utf-8" >
<result>
<a></a>
<c></c>
</result>


Answer this question

XSD how to ensure the presence of one element ?

  • sy_mbm

    This content model only allows

    Sequnce of any, b , c. It does not allow just b, or b, x, y x, b, y etc.

    If that works for you it is ok, but as I said before your requirements were not clear.



  • MAKU

    Thank you for replying to my question.

    The solution using minOccurs / maxOccurs is not a good one for my situation because I don't know the name of the elements (excepts <b>).
    Doing the job with XPath seam to be the last way to achieve it. But this doesn't fit in my solution philosophy...
    The <any > works fine for elements at the end of a sequence ...

    Finally, I have found a solution to this problem (many thanks to http://erwy.developpez.com/):
    It uses <xs:any processContents="lax"/> which allows any xml tree.


    < xml version="1.0" encoding="UTF-8" >
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="root">
    <xs:complexType>
    <xs:sequence>
    <xs:any processContents="lax"/>
    <xs:element name="b"/>
    <xs:element name="c"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    Thanks again!
    And nice XSD time to everyone.

  • wlanni

    You are right. I miss-writed it!
    I meant "No matters if <c/> is there or not".
    Same error for "The problem is that I also have to allow any element in place of <a/> and <b/>."
    I meant "
    The problem is that I also have to allow any element in place of <a/> and <c/>."

    PS: I have edited the first post to reflect these corrections.

  • Jocker23

    It is still unclear what you want to achieve.

    Is what you want any content model that has <b> in it

    Is <x/><y/> <b/> ok Can b or other elements occur more than once Can you have just <b/>

    This type of validation is not easy using XSD unless you know all the elements that can occur in the content model beforehand, at least for the targetnamespace of the element (<b>) in that case you can come up something like

    <group name="allButB">

    <choice minOccurs="0" maxOccurs="unbounded">

    <element name="a1">

    <element name="a2">

    .....<!-- all the other elements in the content model-->

    <element name="aN">

    <element .....>

    <any namespace="#other">

    </choice>

    </group>

    <complexType name="atleastoneB">

    <sequence>

    <group ref="allButB" minOccurs="0" maxOccurs="unbounded"/>

    <element name="b"/>

    <any minOccurs="0" maxOccurs="unbounded"/>

    </sequence>

    </complexType>

    But this might be easier if you just check for existence of b in your code, or use XPath for example ( i.e. check if //b returns anything).



  • Brakenjan

    Is there really nobody who can help me

  • Trevor W. Baltimore

    Arbiorix wrote:


    Let's say that I have a simple xml file with 3 elements <a/>, <b/> and <c/>.
    The xml file is valid for my application if it has the <b/> element. No matters if <a/> is there or not. No matters if <b/> is there or not. The problem is that I also have to allow any element in place of <a/> and <b/>.
    I could receive an xml file with the 3 following elements <aa/><b/><c/>.
    How do I have to write my .xsd

    Your requirement is not clear as you say "the xml file is valid if it has a b element" and contradict that with "no matters if b is there or not".



  • XSD how to ensure the presence of one element ?