来自使用 XMLTask 的 ant 的 XMLPath 无法匹配 XML 文件在不同名称空间中的元素



我试图使用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}解析为数字,并已尝试将其更改为数字,但效果相同。知道吗?

这是我正在使用的路径表达式:

/project/configuration/data/class-path-extension[1]/runtime-relative-path/text()

处理非空(默认)命名空间中未固定名称的一种方法是将名称指定为谓词。

下面是一个用这种风格编写的XPath表达式,用于选择所需的节点:

   /*[name()='project']
     /*[name()='configuration']
       /*[name()='data']
         /*[name()='class-path-extension'][1]
           /*[name()='runtime-relative-path']
             /text()

基于XSLT的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="/">
  <xsl:copy-of select=
  "/*[name()='project']
     /*[name()='configuration']
       /*[name()='data']
         /*[name()='class-path-extension'][1]
           /*[name()='runtime-relative-path']
             /text()
  "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于所提供的XML文档时

<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>

计算XPath表达式,并将所选节点复制到输出

ext/extension/x.jar

注意:对于XPath表达式的增量构建和验证,可以使用诸如XPath可视化工具之类的工具。这个工具已经帮助成千上万的人以有趣的方式学习XPath。

问题是projectdata元素是在不同的命名空间中声明的。您可以通过以下方式在节点谓词中使用local-name()属性:

/*[local-name() = 'project'] ...

您也可以使用namespace-uri属性指定名称空间:

/*[local-name() = 'project' and namespace-uri() = 'http://www.netbeans.org/ns/project/1'] ...

等等。

相关内容

  • 没有找到相关文章

最新更新