mat*_*rns 6 xml ant xpath xml-namespaces
我的目标是简单地更新简单的站点地图xml文档中的"lastmod"节点:
<?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>
Run Code Online (Sandbox Code Playgroud)
我想在部署ant脚本中执行此操作,因此我使用Ant任务XMLTask.这是我的蚂蚁目标:
<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>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的xpath无法匹配任何东西:
[xmltask] TextAction(new text) (/urlset/url/lastmod/text()) failed to match
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下xpath查询,没有运气:
//lastmod/text()
/urlset[@*]/url/lastmod/text()
/urlset[@xmlns]/url/lastmod/text()
Run Code Online (Sandbox Code Playgroud)
但是我发现如果我从源文件中的urlset节点手动删除namespace属性,一切正常.这是XMLTask中的错误还是我做错了什么?
您需要通过冒号前缀告诉 XPath 所有节点都有一个名称空间。正确的表达是:
/:urlset/:url/:lastmod/text()
Run Code Online (Sandbox Code Playgroud)