使用ant更新xml元素

使用ant更新xml元素,xml,xslt,ant,ant-contrib,Xml,Xslt,Ant,Ant Contrib,我有一个XSL文件,它充当我的应用程序的配置文件。事实上,它是一个XML文件,它的周围包裹着元素。此文件名为Config.xsl: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.example.org/Config"> <

我有一个XSL文件,它充当我的应用程序的配置文件。事实上,它是一个XML文件,它的周围包裹着元素。此文件名为Config.xsl:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"           xmlns="http://www.example.org/Config">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"               standalone="yes" />
 <xsl:template match="/">
 <Config>
      <Test>somevalue</Test>
      <Test1>someothervalue</Test1>
 </Config>
 </xsl:template>

一些价值
其他价值

我想用一个新值更改元素Test1的值

下面是我的ant代码,我正在使用它来更新这些值

<?xml version="1.0" encoding="UTF-8" ?>
<project name="Scripts" default="test">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<target name="test">
    <xmltask source="Config.xsl" dest="Config.xsl">
        <replace path="Config/Test1/text()" withText="newvalue" />
    </xmltask>
</target>
</project>


如果有人能告诉我如何完成这项工作,我将不胜感激。

您似乎对名称空间感到困惑。你必须在更换任何东西之前处理好它。有关XML任务如何处理它的更多详细信息,请转到
https://today.java.net/pub/a/today/2006/11/01/xml-manipulation-using-xmltask.html#paths-和名称空间
。但是,您可以使用此代码来获得所需的输出:

输入:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.example.org/Config">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" standalone="yes"/>
  <xsl:template match="/">
    <Config>
      <Test>somevalue</Test>
      <Test1>someothervalue</Test1>
    </Config>
  </xsl:template>
</xsl:stylesheet>

一些价值
其他价值
ANT脚本:

<project name="XML-VALIDATION" default="main" basedir=".">
  <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
  <target name="main">
    <xmltask source="config.xsl" dest="output.xml">
      <replace path="//:Config/:Test1/text()">xxxxxxx</replace>
    </xmltask>
  </target>
</project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.example.org/Config" version="1.0">
  <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="yes" version="1.0"/>
  <xsl:template match="/">
    <Config>
      <Test>somevalue</Test>
      <Test1>xxxxxxx</Test1>
    </Config>
  </xsl:template>
</xsl:stylesheet>

xxxxxxx
输出:

<project name="XML-VALIDATION" default="main" basedir=".">
  <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
  <target name="main">
    <xmltask source="config.xsl" dest="output.xml">
      <replace path="//:Config/:Test1/text()">xxxxxxx</replace>
    </xmltask>
  </target>
</project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.example.org/Config" version="1.0">
  <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="yes" version="1.0"/>
  <xsl:template match="/">
    <Config>
      <Test>somevalue</Test>
      <Test1>xxxxxxx</Test1>
    </Config>
  </xsl:template>
</xsl:stylesheet>

一些价值
xxxxxxx

谢谢你,纳文。这很有魅力。此外,我正在尝试更新同一个文件,以便我的源文件和座位文件都是XSL文件,并且它确实可以工作。