【发布时间】:2015-12-17 17:16:26
【问题描述】:
我有 XML 文档,下面有片段
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>myapp.applications</groupId>
<artifactId>myartifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
我想选择groupId 的值,类似于
xml.xpath("//project/parent/groupId").text
这不是所有属性的结果。我想。
如果将文档更改为
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>myapp.applications</groupId>
<artifactId>myartifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
确实有效。
看来 XPath 表达式对于具有属性的项目元素无效。
什么是正确的 XPath 表达式?
【问题讨论】: