使用以下代码,一切都按预期工作,并且我在跟踪打印输出中保留了默认命名空间和 xsi 命名空间。
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);
但是,如果我在我的 mxml 文件中使用以下代码从外部加载我的 xml:
<fx:Declarations>
<fx:XML xmlns="" id="script" source="script.xml"/>
</fx:Declarations>
那么这个打印的结果最终会删除 xsi 命名空间。我该怎么做才能保留它?谢谢。
var scriptParent:XML = <xml></xml>;
scriptParent.appendChild(script);
trace ("scriptParent " + scriptParent);
最佳答案
我过去曾遇到过这个问题,虽然我恐怕无法回答为什么会发生这种情况,但似乎 Flex 从导入的 XML 中剥离了任何“未使用”的命名空间(即没有子节点直接引用命名空间)。以下与您的问题类似的示例说明了这一点:
文本.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>
使用与上面概述的方法相同的方法,输出如下:
<xml>
<objects xmlns="http://www.spicefactory.org/parsley">
<myTestNs:test xmlns:myTestNs="http://test.com">Test text node</myTestNs:test>
</objects>
</xml>
如果有人能解释为什么会发生这种情况,我会很感兴趣,因为有很多用例我们希望维护所有命名空间声明,即使它们当前未使用。
关于XML 对象删除命名空间 Flex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5054733/