如果 XML 文件具有不同名称空间中的元素,则使用 XMLTasks 的 ant 的 XMLPath 无法匹配

发布于 2025-01-07 17:03:10 字数 3977 浏览 0 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2025-01-14 17:03:10

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

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

And this is the path expression I'm using:

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

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

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

XSLT - based verification:

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

When this transformation is applied on the provided XML document:

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

the XPath expression is evaluated and the selected node is copied to the output:

ext/extension/x.jar

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.

神经暖 2025-01-14 17:03:10

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

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

您还可以指定使用 namespace-uri 属性的命名空间:

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

等等。

The problem is that project and data elements are declared in a different namespaces. You may use local-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文