使用 Ant 在 xml 文档中插入 XML 元素

2023-12-07

我想在 xml 文档中插入一个 xml 元素:-

输入 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>

这意味着我正在尝试再插入一个带有命名空间的内容。我需要完全像这样插入..

我正在尝试下面

   <xmltask source="abc.xml" dest="abc.xml">
<insert path="//*[local-name()='resourceTypes']"> <![CDATA[
        <xa:resourceTypes id="3"/>
        ]]>
    </insert>
    </xmltask>

然而,它失败了。


To get <insert>要工作,请显式引用元素中的名称空间。还<insert>默认情况下,将新元素放在现有元素下。下面的代码将默认值更改为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:

<?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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Ant 在 xml 文档中插入 XML 元素 的相关文章

随机推荐