views:

480

answers:

3

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.

A: 

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.

Jacob
+1  A: 

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:

XML & XSD 1

XML & XSD 2

bug-a-lot
Oh shoot, sorry for answering with pretty much the same answer! (albeit MUCH longer)
macke
No problem, it probably took a long time to write...
bug-a-lot
+1  A: 

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!

macke
I'm starting to wonder if there's anything in the Flex framework that works flawlessly... Thank you for the answer.
bug-a-lot
No problem. The Flex framework is very rushed in a lot of areas, you can tell. Fortunately, it's very easy to roll your own framework pretty quickly and Flex can really be very inspirational. If nothing else than how not to do it. To be fair, Flex has a lot of nice things to, such as MXML which is golden for dependancy injection. I only wish the language would evolve some along with better compiler support for metadata and custom code generation. Anyway, another story for another time.
macke