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?
You need to tell XPath that all nodes have a namespace by prefixing with a colon. The correct expression is: