如果 XML 文件具有不同名称空间中的元素,则使用 XMLTasks 的 ant 的 XMLPath 无法匹配
我试图使用 XMLTasks 来替换 xml 文件中的某些值,但由于不匹配,它一直失败。使用其他工具它说我的 xpath 是正确的,但我无法弄清楚出了什么问题。
这是我正在搜索的文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.apisupport.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
<code-name-base>simple.server.extension.cardgame</code-name-base>
<suite-component/>
<module-dependencies>
<dependency>
<code-name-base>marauroa.lib</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>3</release-version>
<specification-version>8</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>simple.server.lib</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0-1</release-version>
<specification-version>0.2</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
<public-packages>
<package>dreamer.card.game</package>
<package>dreamer.card.game.model.events</package>
<package>dreamer.card.game.price</package>
<package>dreamer.card.game.storage</package>
<package>simple.server.extension</package>
<package>simple.server.extension.card</package>
</public-packages>
<class-path-extension>
<runtime-relative-path>ext/extension/x.jar</runtime-relative-path>
<binary-origin>../../Simple Marauroa Java/Card Game Extension/dist/x.jar</binary-origin>
</class-path-extension>
<class-path-extension>
<runtime-relative-path>ext/extension/y.jar</runtime-relative-path>
<binary-origin>../../Simple Marauroa Java/Card Game interface/dist/y.jar</binary-origin>
</class-path-extension>
</data>
</configuration>
</project>
这是我正在使用的路径表达式:
/project/configuration/data/class-path-extension[1]/runtime-relative-path/text()
这是我尝试运行的任务的相关部分:
<target name="s" depends="-define-xmltasks">
<propertyselector property="subprojects" match="original.project.dir(.*)" select="\1"/>
<for list="${subprojects}" param="subproject">
<sequential>
<xmltask source="nbproject/project.xml" dest="nbproject/project.xml" failWithoutMatch="true">
<replace path="/project/configuration/data/class-path-extension[@{subproject}]/runtime-relative-path/text()"
withText="ext/extension/${extension-lib@{subproject}.dist.jar}"/>
<replace path="/project/configuration/data/class-path-extension[@{subproject}]/binary-origin/text()"
withText="${original.project.dir@{subproject}}/dist/${extension-lib@{subproject}.dist.jar}"/>
</xmltask>
</sequential>
</for>
</target>
@{subproject} 解析为一个数字,并且已经尝试将其更改为数字但具有相同的效果。有什么想法吗?
I'm trying to get some matching using XMLTasks to replace some values in a xml file but it keeps failing due to no match. Using other tools it says that my xpath is correct but I can't figure out what's wrong.
Here the file I'm searching:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.apisupport.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
<code-name-base>simple.server.extension.cardgame</code-name-base>
<suite-component/>
<module-dependencies>
<dependency>
<code-name-base>marauroa.lib</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>3</release-version>
<specification-version>8</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>simple.server.lib</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0-1</release-version>
<specification-version>0.2</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
<public-packages>
<package>dreamer.card.game</package>
<package>dreamer.card.game.model.events</package>
<package>dreamer.card.game.price</package>
<package>dreamer.card.game.storage</package>
<package>simple.server.extension</package>
<package>simple.server.extension.card</package>
</public-packages>
<class-path-extension>
<runtime-relative-path>ext/extension/x.jar</runtime-relative-path>
<binary-origin>../../Simple Marauroa Java/Card Game Extension/dist/x.jar</binary-origin>
</class-path-extension>
<class-path-extension>
<runtime-relative-path>ext/extension/y.jar</runtime-relative-path>
<binary-origin>../../Simple Marauroa Java/Card Game interface/dist/y.jar</binary-origin>
</class-path-extension>
</data>
</configuration>
</project>
And this is the path expression I'm using:
/project/configuration/data/class-path-extension[1]/runtime-relative-path/text()
Here are the relevant parts of the task I'm trying to run:
<target name="s" depends="-define-xmltasks">
<propertyselector property="subprojects" match="original.project.dir(.*)" select="\1"/>
<for list="${subprojects}" param="subproject">
<sequential>
<xmltask source="nbproject/project.xml" dest="nbproject/project.xml" failWithoutMatch="true">
<replace path="/project/configuration/data/class-path-extension[@{subproject}]/runtime-relative-path/text()"
withText="ext/extension/${extension-lib@{subproject}.dist.jar}"/>
<replace path="/project/configuration/data/class-path-extension[@{subproject}]/binary-origin/text()"
withText="${original.project.dir@{subproject}}/dist/${extension-lib@{subproject}.dist.jar}"/>
</xmltask>
</sequential>
</for>
</target>
@{subproject} resolves to a number and already tried changing it for a number but has the same effect. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理非空(默认)命名空间中的无前缀名称的一种方法是将名称指定为谓词。
这是以此样式编写的 XPath 表达式,用于选择所需的节点:
基于 XSLT 的验证:
当此转换应用于提供的 XML 文档时:
计算 XPath 表达式并将所选节点复制到输出:
注意:对于 XPath 表达式的增量构建和验证,您可以使用诸如作为<强>XPath 可视化工具。该工具已帮助成千上万的人以有趣的方式学习 XPath。
One way to deal with unprefixed names that are in a non-empty (default) namespace, is to specify the name as a predicate.
Here is an XPath expression written in this style that selects the wanted node(s):
XSLT - based verification:
When this transformation is applied on the provided XML document:
the XPath expression is evaluated and the selected node is copied to the output:
Note: For incremental building and verification of XPath expressions you can use a tool such as the XPath visualizer. This tool has helped many thousands of people learn XPath the fun way.
问题在于
project
和data
元素是在不同的命名空间中声明的。您可以通过以下方式在节点谓词中使用local-name()
属性:/*[local-name() = 'project'] ...
您还可以指定使用
namespace-uri
属性的命名空间:/*[local-name() = 'project' 和 namespace-uri() = 'http://www.netbeans.org/ns/ project/1'] ...
等等。
The problem is that
project
anddata
elements are declared in a different namespaces. You may uselocal-name()
attribute in node predicate in following way:/*[local-name() = 'project'] ...
You may also specify a namespace using
namespace-uri
attribute:/*[local-name() = 'project' and namespace-uri() = 'http://www.netbeans.org/ns/project/1'] ...
and so on.