By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

correct use of "default" attribute in XML Schema

P: n/a
Hi all,

what is the correct use of the "default" attribute in XML Schema?

For example:
<xs:element name="myProperty" type="xs:string" default="myDefaultValue"/>

What can I do with it? What is the meaning of <
....default="myDefaultValue" /> ?

Thank you so much.

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it

Nov 12 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Default means there is an application that

preempts possible words in xml.

It will expect alignment to BIG MONEY "standard" way and methods

and will not give a "notch".

For xml "USER" settings, regular xml DEVELOPERS,
and seekers of interoperability via xml -- the death sentence .

********

It stems from US neo-texan vision aka death or alive

==========

You read it here first.

==============================
==============================
Now go away and waste other people's time.
You 10 line page only has a flash object embedded
that does not work on my PC, because I have set the Kill Bit
for such crap.

"Stefano" <st****************@giasoft.it> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi all,

what is the correct use of the "default" attribute in XML Schema?

For example:
<xs:element name="myProperty" type="xs:string" default="myDefaultValue"/>

What can I do with it? What is the meaning of <
...default="myDefaultValue" /> ?

Thank you so much.

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it


Nov 12 '05 #2

P: n/a
What?

Excuse me, but I didn't understand what you wrote...

Thanks

Bye

"name" <user@nospam> ha scritto nel messaggio
news:ua**************@TK2MSFTNGP14.phx.gbl...
Default means there is an application that

preempts possible words in xml.

It will expect alignment to BIG MONEY "standard" way and methods

and will not give a "notch".

For xml "USER" settings, regular xml DEVELOPERS,
and seekers of interoperability via xml -- the death sentence .

********

It stems from US neo-texan vision aka death or alive

==========

You read it here first.

==============================
==============================
Now go away and waste other people's time.
You 10 line page only has a flash object embedded
that does not work on my PC, because I have set the Kill Bit
for such crap.

"Stefano" <st****************@giasoft.it> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi all,

what is the correct use of the "default" attribute in XML Schema?

For example:
<xs:element name="myProperty" type="xs:string" default="myDefaultValue"/>
What can I do with it? What is the meaning of <
...default="myDefaultValue" /> ?

Thank you so much.

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it

Nov 12 '05 #3

P: n/a


Stefano wrote:

what is the correct use of the "default" attribute in XML Schema?

For example:
<xs:element name="myProperty" type="xs:string" default="myDefaultValue"/>

What can I do with it? What is the meaning of <
...default="myDefaultValue" /> ?


The documentation is here:
<http://www.w3.org/TR/xmlschema-1/#cElement_Declarations>
That explains that you can put a value constraint on an element. If the
element in the XML file is empty for instance then nevertheless if a
default value is provided in the element declaration that value becomes
the "schema normalized value" in the so called "post-schema-validation
infoset".
For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value.
However I am not sure MSXML 4 or .NET do actually add such a default
value for an element, in a quick test here the value was not added.
I have also tried the Java parser and DOM with Java 1.5 and there the
default value for an element was added and appeared in the DOM.

Both MSXML 4 and .NET however add default values for attributes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #4

P: n/a
Any example about the use of "default" in both elements and attributes
definition?

Thanks a lot.

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it
"Martin Honnen" <ma*******@yahoo.de> ha scritto nel messaggio
news:uF**************@tk2msftngp13.phx.gbl...


Stefano wrote:

what is the correct use of the "default" attribute in XML Schema?

For example:
<xs:element name="myProperty" type="xs:string" default="myDefaultValue"/>
What can I do with it? What is the meaning of <
...default="myDefaultValue" /> ?


The documentation is here:
<http://www.w3.org/TR/xmlschema-1/#cElement_Declarations>
That explains that you can put a value constraint on an element. If the
element in the XML file is empty for instance then nevertheless if a
default value is provided in the element declaration that value becomes
the "schema normalized value" in the so called "post-schema-validation
infoset".
For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value.
However I am not sure MSXML 4 or .NET do actually add such a default
value for an element, in a quick test here the value was not added.
I have also tried the Java parser and DOM with Java 1.5 and there the
default value for an element was added and appeared in the DOM.

Both MSXML 4 and .NET however add default values for attributes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #5

P: n/a


Stefano wrote:
Any example about the use of "default" in both elements and attributes
definition?


You yourself have given an example with a default value for an element
in your original post.

As for an attribute e.g.
<xs:element name="element-name">
<xs:complexType>
<xs:sequence>
<!-- define some element content here -->
</xs:sequence>
<xs:attribute name="attribute-name" type="xs:string"
default="Kibology" />
</xs:complexType>
</xs:element>

For an example focussed introduction to W3C XML schema have a look at
<http://www.w3.org/TR/xmlschema-0/>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #6

P: n/a
I mean... an example using a DOM: MSXML or .NET.

You said:
"For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value."

Can you show me an example about it?

Thanks!

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it
"Martin Honnen" <ma*******@yahoo.de> ha scritto nel messaggio
news:uv**************@tk2msftngp13.phx.gbl...


Stefano wrote:
Any example about the use of "default" in both elements and attributes
definition?


You yourself have given an example with a default value for an element
in your original post.

As for an attribute e.g.
<xs:element name="element-name">
<xs:complexType>
<xs:sequence>
<!-- define some element content here -->
</xs:sequence>
<xs:attribute name="attribute-name" type="xs:string"
default="Kibology" />
</xs:complexType>
</xs:element>

For an example focussed introduction to W3C XML schema have a look at
<http://www.w3.org/TR/xmlschema-0/>

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #7

P: n/a


Stefano wrote:
an example using a DOM: MSXML or .NET.

You said:
"For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value."


I also said that it doesn't work for me with elements for MSXML 4 or
..NET, it is not clear whether that could be regarded a bug or not.
Oleg or anyone else reading, what do you think, should the default value
defined for an element in an XML schema result in the default value
showing up in the DOM with MSXML 4 or .NET?

But it works with attributes, for example if the schema document is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="GOD">
<xs:complexType>
<xs:attribute name="name" default="Kibo" type="xs:string" />
</xs:complexType>
</xs:element>

</xs:schema>

and the XML document is

<?xml version="1.0" encoding="UTF-8"?>
<GOD />

then the following JScript program using MSXML 4 shows that with
validation against the schema turned on the attribute with the default
value is added:

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;

var xmlURL = 'test2005032202.xml';
var schemaURL = 'test2005032202Xsd.xml';

var wellFormed = xmlDocument.load(xmlURL);

if (wellFormed) {
// no validation, yields null for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}

var xmlSchemaCache = new ActiveXObject('Msxml2.XMLSchemaCache.4.0');
xmlSchemaCache.add('', schemaURL);

xmlDocument.schemas = xmlSchemaCache;

var valid = xmlDocument.load(xmlURL);

if (valid) {
// with validation yields Kibo for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}
A similar C# .NET program would be:

const string xmlURL = @"test2005032202.xml";
const string schemaURL = @"test2005032202Xsd.xml";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(xmlURL);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

XmlValidatingReader xmlValidator = new XmlValidatingReader(new
XmlTextReader(xmlURL));
xmlValidator.Schemas.Add(null, schemaURL);

xmlDocument.Load(xmlValidator);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

Output then is

name: .
name: Kibo.

so there the validation ensures that the attribute with the default
value is added.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #8

P: n/a
It's great!

Thank you so much.

Another question...

I tried to add another alert, after the loading of XSD.
alert('document: \n' + xmlDocument.documentElement.xml);

This alert displays:
<GOD />
not
<GOD name="Kibo" />
as I expected.

It's like "xmlDocument.documentElement.xml" property isn't affected by the
applying of the schema.
Only "xmlDocument.documentElement.getAttribute('nam e')" works, retreiving
the default value.

Can you say me why this happen?

My goal is to retrieve a string containing the xml representation with
default values for attributes.

Thank you so much!
--
Stefano Giannone
MCP .NET
http://www.giasoft.it
"Martin Honnen" <ma*******@yahoo.de> ha scritto nel messaggio
news:%2******************@TK2MSFTNGP15.phx.gbl...


Stefano wrote:
an example using a DOM: MSXML or .NET.

You said:
"For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value."


I also said that it doesn't work for me with elements for MSXML 4 or
.NET, it is not clear whether that could be regarded a bug or not.
Oleg or anyone else reading, what do you think, should the default value
defined for an element in an XML schema result in the default value
showing up in the DOM with MSXML 4 or .NET?

But it works with attributes, for example if the schema document is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="GOD">
<xs:complexType>
<xs:attribute name="name" default="Kibo" type="xs:string" />
</xs:complexType>
</xs:element>

</xs:schema>

and the XML document is

<?xml version="1.0" encoding="UTF-8"?>
<GOD />

then the following JScript program using MSXML 4 shows that with
validation against the schema turned on the attribute with the default
value is added:

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;

var xmlURL = 'test2005032202.xml';
var schemaURL = 'test2005032202Xsd.xml';

var wellFormed = xmlDocument.load(xmlURL);

if (wellFormed) {
// no validation, yields null for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}

var xmlSchemaCache = new ActiveXObject('Msxml2.XMLSchemaCache.4.0');
xmlSchemaCache.add('', schemaURL);

xmlDocument.schemas = xmlSchemaCache;

var valid = xmlDocument.load(xmlURL);

if (valid) {
// with validation yields Kibo for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}
A similar C# .NET program would be:

const string xmlURL = @"test2005032202.xml";
const string schemaURL = @"test2005032202Xsd.xml";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(xmlURL);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

XmlValidatingReader xmlValidator = new XmlValidatingReader(new
XmlTextReader(xmlURL));
xmlValidator.Schemas.Add(null, schemaURL);

xmlDocument.Load(xmlValidator);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

Output then is

name: .
name: Kibo.

so there the validation ensures that the attribute with the default
value is added.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #9

P: n/a
Another question...

I see that only MSXML 3.0 is shipped with IE 6... right?

So, the client needs to install MSXML 4 to run the javascript you suggested
me.

Can you suggest me a version of that javascript that uses MSXML 3.0 ?
(I simple tried to change 4 to 3 in the new ActiveXObject, but it doesn't
work)

Thank you so much!

Bye
--
Stefano Giannone
MCP .NET
http://www.giasoft.it
"Martin Honnen" <ma*******@yahoo.de> ha scritto nel messaggio
news:%2******************@TK2MSFTNGP15.phx.gbl...


Stefano wrote:
an example using a DOM: MSXML or .NET.

You said:
"For instance if you build a DOM then the DOM of such an element could
contain the default value even if the XML input markup doesn't contain
that value."


I also said that it doesn't work for me with elements for MSXML 4 or
.NET, it is not clear whether that could be regarded a bug or not.
Oleg or anyone else reading, what do you think, should the default value
defined for an element in an XML schema result in the default value
showing up in the DOM with MSXML 4 or .NET?

But it works with attributes, for example if the schema document is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="GOD">
<xs:complexType>
<xs:attribute name="name" default="Kibo" type="xs:string" />
</xs:complexType>
</xs:element>

</xs:schema>

and the XML document is

<?xml version="1.0" encoding="UTF-8"?>
<GOD />

then the following JScript program using MSXML 4 shows that with
validation against the schema turned on the attribute with the default
value is added:

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;

var xmlURL = 'test2005032202.xml';
var schemaURL = 'test2005032202Xsd.xml';

var wellFormed = xmlDocument.load(xmlURL);

if (wellFormed) {
// no validation, yields null for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}

var xmlSchemaCache = new ActiveXObject('Msxml2.XMLSchemaCache.4.0');
xmlSchemaCache.add('', schemaURL);

xmlDocument.schemas = xmlSchemaCache;

var valid = xmlDocument.load(xmlURL);

if (valid) {
// with validation yields Kibo for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
}
A similar C# .NET program would be:

const string xmlURL = @"test2005032202.xml";
const string schemaURL = @"test2005032202Xsd.xml";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(xmlURL);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

XmlValidatingReader xmlValidator = new XmlValidatingReader(new
XmlTextReader(xmlURL));
xmlValidator.Schemas.Add(null, schemaURL);

xmlDocument.Load(xmlValidator);

Console.WriteLine("name: {0}.",
xmlDocument.DocumentElement.GetAttribute("name"));

Output then is

name: .
name: Kibo.

so there the validation ensures that the attribute with the default
value is added.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #10

P: n/a


Stefano wrote:

I tried to add another alert, after the loading of XSD.
alert('document: \n' + xmlDocument.documentElement.xml);

This alert displays:
<GOD />
not
<GOD name="Kibo" />
as I expected.

It's like "xmlDocument.documentElement.xml" property isn't affected by the
applying of the schema.


It seems the serializer for the xml property in MSXML doesn't serialize
default attributes, the documentation at
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmproxml.asp>
does not tell whether or not default attributes should be serialized.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #11

P: n/a

Stefano wrote:
I see that only MSXML 3.0 is shipped with IE 6... right?
That is correct.
So, the client needs to install MSXML 4 to run the javascript you suggested
me.
If you want to have an MSXML version that supports W3C XML schemas then
you need to have at least MSXML 4, MSXML 3 only supports DTDs (and so
called XDR schemas) but not W3C XML schemas.
Can you suggest me a version of that javascript that uses MSXML 3.0 ?


A DTD allows you to define default values for attributes too, here is an
example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE GOD [
<!ELEMENT GOD EMPTY>
<!ATTLIST GOD
name CDATA "Kibo">
]>
<GOD />

Then parsed with MSXML 3 the program

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDocument.async = false;

var xmlURL = 'test2005032301.xml';

var valid = xmlDocument.load(xmlURL);

if (valid) {
// with validation yields Kibo for attribute value
alert('name: ' + xmlDocument.documentElement.getAttribute('name'));
alert(xmlDocument.xml);
}
else {
alert(xmlDocument.parseError.reason);
}

again shows the attribute value 'Kibo' taken from the DTD but also shows
that in this case too the default attribute is not serialized.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #12

P: n/a
Great answer!

Thank you so much!

--
Stefano Giannone
http://www.giasoft.it
Nov 12 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.