XML Schema with variable number of attributes

Discussion in 'XML' started by ashish.sadanandan@gmail.com, Jun 20, 2007.

  1. Guest

    Hi,
    I'm new to XML and just started reading some documentation on W3C
    compliant schemas recently. The XML file I'm generating is for an
    embedded application and so size is of extreme importance. The
    information I want to save in this XML file is the attributes of the
    members of a structure.

    Now lets say there is a member variable that is an array (of length
    10), so I'd want the following line in that variable's attributes:

    <width>10</width>

    Now if I have another variable that is not an array then I'd like to
    avoid the width specification altogether rather than have:

    <width>1</width>

    Does the W3C schema support a variable number of attributes so that
    the parser can assign default values for the missing attributes? If it
    doesn't then can someone please point me to a schema format and a
    parser that does support this?

    I am currently planning on using the Xerxes parser.

    Thanks in advance,
    Ashish.
     
    , Jun 20, 2007
    #1
    1. Advertising

  2. wrote:

    > Now lets say there is a member variable that is an array (of length
    > 10), so I'd want the following line in that variable's attributes:
    >
    > <width>10</width>
    >
    > Now if I have another variable that is not an array then I'd like to
    > avoid the width specification altogether rather than have:
    >
    > <width>1</width>
    >
    > Does the W3C schema support a variable number of attributes so that
    > the parser can assign default values for the missing attributes?


    I don't understand what you are asking about. In XML terminology
    <width>10</width>
    is an element with name 'width' and simple contents '10' so there is no
    attribute at all in your example.
    If you want to know whether you can define default values for elements
    with W3C XSD schemas then the answer is yes, you can do that:
    <xs:element name="width" type="xs:int" default="1"/>
    However the element needs to be present but empty for the default value
    to be applied.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 20, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 20, 9:39 am, Martin Honnen <> wrote:
    > wrote:
    > > Now lets say there is a member variable that is an array (of length
    > > 10), so I'd want the following line in that variable's attributes:

    >
    > > <width>10</width>

    >
    > > Now if I have another variable that is not an array then I'd like to
    > > avoid the width specification altogether rather than have:

    >
    > > <width>1</width>

    >
    > > Does the W3C schema support a variable number of attributes so that
    > > the parser can assign default values for the missing attributes?

    >
    > I don't understand what you are asking about. In XML terminology
    > <width>10</width>
    > is an element with name 'width' and simple contents '10' so there is no
    > attribute at all in your example.
    > If you want to know whether you can define default values for elements
    > with W3C XSD schemas then the answer is yes, you can do that:
    > <xs:element name="width" type="xs:int" default="1"/>
    > However the element needs to be present but empty for the default value
    > to be applied.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Martin,
    I shouldn't have used the word attribute. What I meant is can I skip
    the element 'width' so that the parser automatically assigns the
    default value? From your answer it seems that I can't if I want the
    schema to be W3C compliant, is there any other schema format that'll
    let me do this?

    Thanks for your response,
    Ashish.
     
    , Jun 20, 2007
    #3
  4. wrote:
    > I shouldn't have used the word attribute. What I meant is can I skip
    > the element 'width' so that the parser automatically assigns the
    > default value? From your answer it seems that I can't if I want the
    > schema to be W3C compliant, is there any other schema format that'll
    > let me do this?


    If you want absence of an element to imply a default value, I'd suggest
    you implement that in the application logic, or in a processing stage
    such as a stylesheet, rather than in the schema.

    Remember: The schema's job is not to define all the semantics of a
    document. It's to define higher-level syntax and to *help* you define
    the semantics.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Jun 20, 2007
    #4
  5. Hi Ashish,

    If you use attributes instead of elements, you can achieve what you want.

    <xs:attribute name="width" type="xs:integer" default="1"/>

    In this case, if you don't provide width attribute in the xml file, the
    processor will insert it and give it the value of 1.

    --
    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
    <> wrote in message
    news:...
    > On Jun 20, 9:39 am, Martin Honnen <> wrote:
    >> wrote:
    >> > Now lets say there is a member variable that is an array (of length
    >> > 10), so I'd want the following line in that variable's attributes:

    >>
    >> > <width>10</width>

    >>
    >> > Now if I have another variable that is not an array then I'd like to
    >> > avoid the width specification altogether rather than have:

    >>
    >> > <width>1</width>

    >>
    >> > Does the W3C schema support a variable number of attributes so that
    >> > the parser can assign default values for the missing attributes?

    >>
    >> I don't understand what you are asking about. In XML terminology
    >> <width>10</width>
    >> is an element with name 'width' and simple contents '10' so there is no
    >> attribute at all in your example.
    >> If you want to know whether you can define default values for elements
    >> with W3C XSD schemas then the answer is yes, you can do that:
    >> <xs:element name="width" type="xs:int" default="1"/>
    >> However the element needs to be present but empty for the default value
    >> to be applied.
    >>
    >> --
    >>
    >> Martin Honnen
    >> http://JavaScript.FAQTs.com/

    >
    > Martin,
    > I shouldn't have used the word attribute. What I meant is can I skip
    > the element 'width' so that the parser automatically assigns the
    > default value? From your answer it seems that I can't if I want the
    > schema to be W3C compliant, is there any other schema format that'll
    > let me do this?
    >
    > Thanks for your response,
    > Ashish.
    >
     
    Stan Kitsis [MSFT], Jun 20, 2007
    #5
    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,661
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    996
    Martin Honnen
    Feb 5, 2004
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,325
    Stanimir Stamenkov
    Apr 25, 2005
  4. Replies:
    3
    Views:
    3,170
  5. Mike
    Replies:
    5
    Views:
    1,349
    Stefan Ram
    Sep 20, 2008
Loading...

Share This Page