Use of required and optional

Discussion in 'XML' started by LJ, Feb 8, 2006.

  1. LJ

    LJ Guest

    I am trying to define two xml attribute in my xsd so that they are
    mutual exclusive and one of them is required.

    For example, if I have two attributes, they are either

    <xsd:attribute name="p" type="xsd:string" use="required" />
    <xsd:attribute name="c" type="xsd:string" use="optional" />
    or

    <xsd:attribute name="p" type="xsd:string" use="optional" />
    <xsd:attribute name="c" type="xsd:string" use="required" />

    how can I specify this in the xsd?

    Thx
     
    LJ, Feb 8, 2006
    #1
    1. Advertising

  2. LJ wrote:

    > I am trying to define two xml attribute in my xsd so that they are
    > mutual exclusive and one of them is required.
    >
    > For example, if I have two attributes, they are either
    >
    > <xsd:attribute name="p" type="xsd:string" use="required" />
    > <xsd:attribute name="c" type="xsd:string" use="optional" />
    > or
    >
    > <xsd:attribute name="p" type="xsd:string" use="optional" />
    > <xsd:attribute name="c" type="xsd:string" use="required" />
    >
    > how can I specify this in the xsd?


    XSD does not allow you to define such constraints between different
    attributes.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 8, 2006
    #2
    1. Advertising

  3. You have a few options.
    1. If they are trully mutually exclusive, you might be able to only use one
    of them
    2. Create two attributes: type (which can be either p or c) and value (which
    will be a string representing the value)
    3. Convert your attributes to elements and use <xs:choice>

    --
    Stan Kitsis
    Program Manager, XML Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm


    "LJ" <> wrote in message
    news:...
    >I am trying to define two xml attribute in my xsd so that they are
    > mutual exclusive and one of them is required.
    >
    > For example, if I have two attributes, they are either
    >
    > <xsd:attribute name="p" type="xsd:string" use="required" />
    > <xsd:attribute name="c" type="xsd:string" use="optional" />
    > or
    >
    > <xsd:attribute name="p" type="xsd:string" use="optional" />
    > <xsd:attribute name="c" type="xsd:string" use="required" />
    >
    > how can I specify this in the xsd?
    >
    > Thx
    >
     
    Stan Kitsis [MSFT], Feb 8, 2006
    #3
  4. LJ

    George Bina Guest

    If you really want to keep the document structure and none of the above
    alternatives solve your problem then you have also the following
    possibilities:

    A. Make both attributes optional and use Schematron embedded rules in
    XML Schema to check that you have only one of them
    B. Use Relax NG instead of XML Schema

    Here it is a sample for case A. XML Schema with embedded Schematron
    rules:
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sch="http://www.ascc.net/xml/schematron">
    <xs:element name="test">
    <xs:annotation>
    <xs:appinfo>
    <sch:pattern name="Check that we have only one attribute.">
    <sch:rule context="test">
    <sch:assert test="count(@p|@c)=1">Ony one of the attributes
    p and c can be
    specified.</sch:assert>
    </sch:rule>
    </sch:pattern>
    </xs:appinfo>
    </xs:annotation>
    <xs:complexType>
    <xs:attribute name="p" type="xs:string" use="optional"/>
    <xs:attribute name="c" type="xs:string" use="optional"/>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    on the document
    <test c="1" p="2"/>

    will give:
    SystemID: C:\george\workspace\oXygen\samples\test.xml
    Location: 1:0
    Description: Ony one of the attributes p and c can be specified.
    (count(@p|@c)=1)

    And here it is a Relax NG sample schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
    <element name="test">
    <choice>
    <attribute name="c">
    <data type="string"/>
    </attribute>
    <attribute name="p">
    <data type="string"/>
    </attribute>
    </choice>
    </element>
    </start>
    </grammar>

    And in compact syntax that is:

    default namespace = ""

    start =
    element test {
    attribute c { xsd:string }
    | attribute p { xsd:string }
    }


    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com
     
    George Bina, Feb 9, 2006
    #4
    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. Sean
    Replies:
    8
    Views:
    395
    Mark Parnell
    Feb 15, 2004
  2. MENTAT
    Replies:
    8
    Views:
    963
    MENTAT
    Apr 4, 2005
  3. Eric DeWall

    Optional use of logging library module

    Eric DeWall, Jun 22, 2004, in forum: Python
    Replies:
    6
    Views:
    295
    Vinay Sajip
    Jul 5, 2004
  4. Philipp
    Replies:
    2
    Views:
    533
    Philipp
    Apr 11, 2008
  5. Spaulding

    Required and optional elements

    Spaulding, Aug 14, 2009, in forum: XML
    Replies:
    1
    Views:
    941
    Martin Honnen
    Aug 15, 2009
Loading...

Share This Page