本文介绍了使用 Ant 在 xml 文档中插入 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 xml 文档中插入一个 xml 元素:-
I want to insert one xml element in xml document :-
输入 XML:-
<cus:try xmlns:cus="http://www.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.efg.com">
<cus:trying>
<cus:query>
<xt:resourceTypes>abc</xt:resourceTypes>
<xt:envValueTypes>def</xt:envValueTypes>
</cus:query>
</cus:trying>
</cus:try>
输出 XML:-
<cus:try xmlns:cus="http://www.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.efg.com">
<cus:trying>
<cus:query>
<xt:resourceTypes>abc</xt:resourceTypes>
<xt:resourceTypes>bcd</xt:resourceTypes>
<xt:envValueTypes>def</xt:envValueTypes>
</cus:query>
</cus:trying>
</cus:try>
这意味着我正在尝试使用命名空间再插入一个.我需要完全像这样插入..
That means i'm trying to insert one more with namespaces. I need to insert exactly like this..
我正在尝试下面
<xmltask source="abc.xml" dest="abc.xml">
<insert path="//*[local-name()='resourceTypes']"> <![CDATA[
<xa:resourceTypes id="3"/>
]]>
</insert>
</xmltask>
然而,它失败了.
推荐答案
要使 工作,请显式引用元素中的命名空间.此外,
,默认情况下,将新元素放在现有元素下.下面的代码将默认值更改为
after
.
To get <insert>
to work, explicitly refer to the namespace in the element. Also <insert>
, by default, puts new elements under existing ones. The code below changes the default to after
.
<xmltask source="abc.xml" dest="abc.xml">
<insert path="//*[local-name()='resourceTypes']" position="after"> <![CDATA[
<xt:resourceTypes xmlns:xt="http://www.efg.com">bcd</xt:resourceTypes>
]]>
</insert>
</xmltask>
基于问题中的 XML 生成的 XML:
The resulting XML, based on the XML in the question:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<cus:try xmlns:cus="http://www.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.efg.com">
<cus:trying>
<cus:query>
<xt:resourceTypes>abc</xt:resourceTypes>
<xt:resourceTypes>bcd</xt:resourceTypes>
<xt:envValueTypes>def</xt:envValueTypes>
</cus:query>
</cus:trying>
</cus:try>
这篇关于使用 Ant 在 xml 文档中插入 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!