Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an XML document with Ant

My goal is to simply update the "lastmod" node in a simple sitemap xml document:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://www.example.com/</loc>
        <lastmod>2005-01-01</lastmod>
    </url>
</urlset> 

I want to do this as part of my deploy ant script so I am using the Ant task XMLTask. Here is my ant target:

<target name="update-sitemap" description="update the update date">
    <xmltask source="war/sitemap.xml" dest="war/newsitemap.xml" report="true">
        <replace path="/urlset/url/lastmod/text()" withText="new text"/>
    </xmltask>
</target>

Unfortunately, my xpath fails to match anything:

[xmltask] TextAction(new text) (/urlset/url/lastmod/text()) failed to match

I have also tried the following xpath queries with no luck:

//lastmod/text()
/urlset[@*]/url/lastmod/text()
/urlset[@xmlns]/url/lastmod/text()

I have however discovered that if I manually remove the namespace attribute from the urlset node in my source file, everything works ok. Is this a bug in XMLTask or am I doing something wrong?

like image 739
matt burns Avatar asked Apr 21 '11 03:04

matt burns


People also ask

How do I update an XML file?

To update data in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows. The entire column value will be replaced. The input to the XML column must be a well-formed XML document.

How do I run an Ant file in XML?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is Ant XML?

Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets. For example, Ant is used in the context of plug-in development in the build.


1 Answers

You need to tell XPath that all nodes have a namespace by prefixing with a colon. The correct expression is:

/:urlset/:url/:lastmod/text()
like image 78
matt burns Avatar answered Sep 28 '22 02:09

matt burns