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/