Fun with xmlns in Flex

So, one of the more interesting things about Flex is that, in addition to Perl-like regular expression literals, it also gives us full-on XML literals. Maybe ‘perverse’ is more accurate than ‘interesting’–the things this must do to the grammar (and the number of issues in Adobe JIRA seems to confirm this). Anyway, it looks like this:

var myXML:XML = <foo><bar baz="quux"><xyzzy/></foo>;

This can be pretty handy to test things when developing something that will eventually consume XML produced by the server. Also, since a number of Flex components (e.g., Menu) consume XML, it’s easy to define it inline.

I am currently experimenting with Degrafa, and using some SVG files as input. Given Flex’s fancy XML support, I decided to just copy and paste the entire text of the SVG file. Everything seems to build fine, but when I launch—no dice. Nothing shows up.

It took me a couple of hours of debugging to track down the problem to this:

var myXML:XML = <foo xmlns="http://www.example.com/foo"><bar baz="quux"><xyzzy/></foo>;

The XML namespace declaration for the XML literal seems to confuse either Flex or Degrafa (I’m not quite sure which), and my Geometry was not being updated. As soon as I took out the xmlns declaration, things are hunky dory.

Update: It seems the problem is in Flex itself. With the following code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
	horizontalAlign="center" verticalAlign="middle">
	<mx:Script>
		<![CDATA[
            private const withNamespace:XML =
                <foo xmlns="http://example.com"><bar>namespaced</bar><baz/></foo>;
            private const withoutNamespace:XML =
                <foo><bar>not namespaced</bar><baz/></foo>;
		]]>
	</mx:Script>
    <mx:Label text="{withNamespace.bar}"/>
    <mx:Label text="{withoutNamespace.bar}"/>
</mx:Application>

only ‘not namespaced’ shows.

Advertisements