使用 xPath 从 pom 中提取版本号时遇到问题
我在从 POM 中提取版本号时遇到问题。有人可以帮我吗?
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>watcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<groupId>com.test.file</groupId>
<name>file-watcher</name>
<packaging>jar</packaging>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<relativePath/>
<version>2.6.1</version>
</parent>
</project>
XPATH 表达式:
//project/version/text()
错误: 这返回未找到匹配项
I'm having issue extracting the version number from POM. Can someone help me with it ?
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>watcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<groupId>com.test.file</groupId>
<name>file-watcher</name>
<packaging>jar</packaging>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<relativePath/>
<version>2.6.1</version>
</parent>
</project>
XPATH Expression:
//project/version/text()
Error: This is returning no match found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的元素位于默认名称空间中。因此,尝试使用与名称空间无关的 XPath-1.0 表达式,如下所示:
它的输出是
根据需要
Your elements are in a default namespace. So try a namespace-agnostic XPath-1.0 expression like the following:
Its output is
as desired.
这里的 XPath 是正确的,它应该返回
0.0.1-SNAPSHOT
它可以在 XPath 模拟器上进行模拟,例如 this
所以很明显你的问题不在于 XPath 表达式,而在于你用它做什么。
Your XPath here is correct, it should return
0.0.1-SNAPSHOT
It can be simulated on XPath simulators like this
So it's quite clear your problem is not with the XPath expression while with what you doing with it.