【问题标题】:XPathExpression to select child attribute value ignoring the namespaceXPathExpression 选择忽略命名空间的子属性值
【发布时间】:2017-01-28 18:10:04
【问题描述】:

我对 XPathExpression 的工作还是很陌生,我正在尝试构建一个表达式来将属性值检索到我的自定义 java 对象中,即 Message。下面是一个虚拟 XML,其中有许多 message 节点。

我的 Java 代码应该读取这个 XML,并相应地为 messages 创建一个 Java 对象列表。

这是我尝试构建 XpathExpression 的条件。

  1. 在此 Xpath jobReport/records/record/messages 中检查 title 是否具有值 TestCompany。如果有任何其他公司消息应跳过

我正在使用下面的 Xpath 表达式,但由于命名空间数据,它无法返回数据。现在我需要使用表达式跳过这些名称空间并获取 Title 值。

  1. jobReport/records/record/messages/*[local-name()='aa:title']
  2. jobReport/records/record/messages/*[local-name()='aa:title'/text()='TestCompany']
  3. //*[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>

【问题讨论】:

    标签: java xml xpath xml-namespaces


    【解决方案1】:

    元素的local-name() 将不包含命名空间前缀。

    所以,不要使用*[local-name()='aa:title'],而是使用*[local-name()='title']

    【讨论】:

    • 谢谢。但这并没有给我我想要的东西。我尝试使用以下表达式,但这只是给我命名空间而不是值,即 TestCompany jobReport/records/record/messages/*[local-name()='alertmethod']
    • 糟糕,这是一个错字,正在更正表达式 /jobReport/records/record/messages/*[local-name()='title']
    • 仔细检查: /jobReport/records/record/messages/*[local-name()='title'] does select aa:title as shown here.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2021-10-02
    • 2015-10-26
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多