XML Schema with variable number of attributes

A

ashish.sadanandan

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.
 
M

Martin Honnen

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.
 
A

ashish.sadanandan

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,
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.
 
J

Joseph Kesselman

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.
 
S

Stan Kitsis [MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top