XSD unordered sequence with undefined elements ignored

Discussion in 'XML' started by klikic, Jan 14, 2007.

  1. klikic

    klikic Guest

    Hi.
    How can create a unordered sequence with defined elements that can
    occur ones and "other" elements that are ignored.
    Example:
    XML:
    <Recur>
    <ignoredElement>saasda</ignoredElement>
    <frequency>sdsad</frequency>
    <recurEnd>kjdskdj</recurEnd>
    </Recur>

    Elements "frequency" and "recurEnd" are not optional and can occur only
    ones. Element "ignoredElement" or any other element taht is not
    defined, can occur multiple times or none, and can be at any position
    (between mandatory elements or at end,...).

    I created XSD:
    <xs:element name="Recur">
    <xs:complexType mixed="false">
    <xs:sequence maxOccurs="unbounded">
    <xs:element ref="frequency"/>
    <xs:element ref="endRecur"/>
    <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"//>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    This doesn't work: I have errors while validating XML with XSD.
    I tried with choice but with same effect.
    Any help or suggestion would be nice.
    Thanks.
    Alan
     
    klikic, Jan 14, 2007
    #1
    1. Advertising

  2. klikic

    klikic Guest

    I am sorry I made a mistake:
    element in XML is not recurEnd but endRecur.

    > <Recur>
    > <ignoredElement>saasda</ignoredElement>
    > <frequency>sdsad</frequency>
    > <recurEnd>kjdskdj</recurEnd>
    > </Recur>
     
    klikic, Jan 14, 2007
    #2
    1. Advertising

  3. klikic

    George Bina Guest

    Hi,

    You cannot do that with XML Schema. All you can do is to relax the
    content model allowing more than what you want, use <xs:choice
    maxOccurs="unbounded">. If Relax NG does not scare you then look into
    that. Alternatively you can add a couple of Schematron rules inside XML
    Schema to enforce one occurrence of frequency and endRecur elements.
    See below a complete example that uses Schematron embedded rules:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com"
    xmlns:t="http://www.example.com">
    <xs:annotation>
    <xs:appinfo>
    <ns xmlns="http://www.ascc.net/xml/schematron"
    uri="http://www.example.com" prefix="t"/>
    </xs:appinfo>
    </xs:annotation>
    <xs:element name="Recur">
    <xs:annotation>
    <xs:appinfo>
    <pattern xmlns="http://www.ascc.net/xml/schematron"
    name="test">
    <rule context="t:Recur">
    <assert test="count(t:frequency)=1">There should be one
    occurrence of frequency.</assert>
    <assert test="count(t:endRecur)=1">There should be one
    occurrence of endRecur.</assert>
    </rule>
    </pattern>
    </xs:appinfo>
    </xs:annotation>
    <xs:complexType mixed="false">
    <xs:choice maxOccurs="unbounded">
    <xs:element name="frequency" form="qualified"
    type="xs:string"/>
    <xs:element name="endRecur" form="qualified"/>
    <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"
    processContents="lax"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com
     
    George Bina, Jan 15, 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.
Similar Threads
  1. Ha
    Replies:
    1
    Views:
    866
    arnold m. slotnik
    Jan 28, 2004
  2. yann
    Replies:
    1
    Views:
    396
    C. M. Sperberg-McQueen
    Aug 19, 2005
  3. oziris
    Replies:
    3
    Views:
    706
    oziris
    Nov 24, 2005
  4. Kaushik
    Replies:
    5
    Views:
    555
    John Ersatznom
    Jan 8, 2007
  5. Replies:
    4
    Views:
    2,083
Loading...

Share This Page