I have an xml with an xml-schema. The xml-schema defines an abstract complex type with 2 optional attributes that have default values. Then I have several complex types that extend the base one. And finally nodes of the types defined. So I load the xml and when I parse each node, the optional attributes are not present at all. I've tried fooling around with the namespaces, even:
XML.ignoreProcessingInstructions = false;
No luck. Something similar was being experienced by this guy on codingforums, but that was like 5 years ago. Same is happening to me with firefox 3.0.11 - the xml is shown without the default attributes. For now I'm setting the default values in code, but isn't there a way to make them available from the xml-schema?
Sample XML-schema:
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myorg.org" xmlns:tns="http://www.myorg.org" elementFormDefault="qualified">
<element name="config" type="tns:FieldsNode"></element>
<complexType name="FieldsNode">
<choice minOccurs="0" maxOccurs="unbounded">
<element name="ImagePicker" type="tns:ImagePickerNode"
maxOccurs="unbounded" minOccurs="0">
</element>
</choice>
</complexType>
<complexType name="FieldBase">
<attribute use="required" name="id" type="string"></attribute>
<attribute use="optional" default="true" name="mandatory"
type="boolean">
</attribute>
<attribute default="3" name="colspan" type="int" use="optional"></attribute>
</complexType>
<complexType name="ImagePickerNode">
<complexContent>
<extension base="tns:FieldBase">
<attribute name="maxWidth" type="int" use="required"></attribute>
<attribute name="maxHeight" type="int" use="required"></attribute>
</extension>
</complexContent>
</complexType>
Sample XML:
<config xmlns="http://www.myorg.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.myorg.org test.xsd">
<ImagePicker id="somePicker" maxHeight="10" maxWidth="12"/>
<ImagePicker id="someOtherPicker" maxHeight="100" maxWidth="212" colspan="1" mandatory="false"/>
</config>
Edit: added sample xml and schema.
Actually, it is possible, this guy wrote up how to use the XMLDecoder and XMLEncoder classes in the flex framework to parse/write xml based on a schema:
http://blog.misprintt.net/?p=181
http://blog.misprintt.net/?p=192
Example application showcasing both parsing and writing: http://misprintt.net/examples/xmlSchema/
However, it should be noted that there are several bugs in these classes which may or may not cause problems for your specific application. One of them, if I recall correctly, is directly related to optional parameters with default values in the schema. In some (or perhaps all) instances, the default value was never set. However, these bugs are usually very easy to fix once you've figured out where in the Encoder/Decoder classes the problems lie (because it's almost always those two classes). It can be tricky to spot the error due to these classes highly recursive nature, but for smaller schemas (and subsequently xml files) it's not really difficult.
You still have to create the ActionScript types for the corresponding schema type though. This could be done somewhat automatically by having the XMLDecoder spit out it's result in an anonymous object and then serializing that object into a JSON format or AS class format directly. However, unless all attributes and elements exist in your xml, you're going to miss out on some properties. There is also this project, which I have yet to evaluate: http://www.graniteds.org/confluence/display/DOC/2.+Gas3+Code+Generator
It supposedly converts Java beans to AS3, and schema to Java beans converters are a dime a dozen these days.
Hope it helps!
Recently stumbled upon the following topic: Validate Xml in Flex3 Air
The answer there by Gregor Kiddie might also answer my question. Basically two links to note as per said answer:
I don't know of any XML libraries that insert default attributes based on a schema into the DOM for an XML file. Certainly, Flex doesn't. I believe the default values specified in a schema is really meant more as documentation for tools, such as those that generate XML serializable classes.