With the following code, everything works as expected, and I retain both the default namespace, and the xsi namespace in the trace printo开发者_StackOverflow中文版ut.
var tempData = <objects xmlns="http://www.spicefactory.org/parsley" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</objects>
var scriptParent:XML = <xml></xml>;
scriptParent.appendChild(tempData);
trace ("scriptParent " + scriptParent);
However, if I load my xml externally using the following code in my mxml file:
<fx:Declarations>
<fx:XML xmlns="" id="script" source="script.xml"/>
</fx:Declarations>
then the results of this print end up dropping the xsi namespace. What can i do to retain it? Thanks.
var scriptParent:XML = <xml></xml>;
scriptParent.appendChild(script);
trace ("scriptParent " + scriptParent);
I've run into this issue in the past and whilst I'm afraid I can't answer why this happens it appears that Flex strips off any "unused" namespaces from imported XML (i.e. no child node references the namespace directly). The following example similar to your question demonstrates this:
Text.xml:
<?xml version="1.0"?>
<objects xmlns="http://www.spicefactory.org/parsley" xmlns:myTestNs="http://test.com" xmlns:myUnusedNs="http://testUnused.com">
<myTestNs:test>Test text node</myTestNs:test>
</objects>
Using the same method as you have outlined above, the output is as follows:
<xml>
<objects xmlns="http://www.spicefactory.org/parsley">
<myTestNs:test xmlns:myTestNs="http://test.com">Test text node</myTestNs:test>
</objects>
</xml>
If anyone can explain why this occurs I'd be interested as there are many use cases where we want to maintain all namespace declarations even if they aren't currently in use.
精彩评论