我对 XPathExpression 的工作还很陌生,我正在尝试构建一个表达式来将属性值检索到我的自定义 java 对象(即 Message)中。下面是虚拟 XML,其中包含许多消息节点。
我的 Java 代码应该读取此 XML,并相应地为消息创建 Java 对象列表。
这是我尝试构建 XpathExpression 的条件。
- 在此 Xpath 中jobReport/records/record/messages 检查title 是否具有值TestCompany。如有其他公司留言请跳过
我正在使用下面的 Xpath 表达式,但由于命名空间数据而无法返回数据。现在我需要使用表达式跳过这些 namespace 并获取 Title 值。
- jobReport/records/record/messages/*[local-name()='aa:title']
- jobReport/records/record/messages/*[local-name()='aa:title'/text()='TestCompany']
- //*[local-name()='aa:title'/text()='TestCompany']
j
<?xml version="1.0" encoding="UTF-8"?>
<jobReport>
<jobID>123515412512221</jobID>
<rundate>2016-09-16</rundate>
<startTime>13:09:49</startTime>
<endTime>13:10:39</endTime>
<containsErrors>false</containsErrors>
<records>
<record>
<recordid>20160920001</recordid>
<primaryfilename>hello.pdf</primaryfilename>
<result>
<status>OK</status>
<errorcode />
</result>
<messages>
<aa:title xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="DummyProject/2016-04">TestCompany</aa:title>
<aa:messageid xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">30.02.02.15.01</aa:messageid>
<aa:messagefrom xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">FI</aa:messagefrom>
<aa:messageto
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">keijo</aa:messageto>
<aa:mailid
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">agreement</aa:mailid>
<aa:phonenumber
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">xml-tuote</aa:phonenumber>
</messages>
</record>
</records>
</jobReport>
最佳答案
元素的 local-name()
不会包含命名空间前缀。
因此,不要使用 *[local-name()='aa:title']
,而是使用 *[local-name()='title']
。
关于java - XPathExpression 选择子属性值忽略命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39600509/