基于ID值的XPath过滤器

时间:2016-12-23 16:11:12

标签: xml xpath domxpath

从XML下面我想要提取Mobile_Number标记下的values/value值。我怎样才能使这个通用,因为我们以后可以有多个选项而不是单Mobile_Number选项。

<?xml version="1.0" encoding="UTF-8"?>
<Request>
  <header>
    <businessTransactionID>ABCD</businessTransactionID>
    <externalCorrelationID>UABCD</externalCorrelationID>
    <sentTimestamp>2016-12-23T14:21:11.261+01:00</sentTimestamp>
    <sourceContext>
      <host>stackoverflow</host>
      <application>browser</application>
      <operation>ExtractXMLTAGVALUE</operation>
    </sourceContext>
  </header>
  <body>
    <serviceCharacteristic>
      <specification>
        <ID>Mobile_Number</ID>
      </specification>
      <values>
        <value>13008421</value>
      </values>
    </serviceCharacteristic>
  </body>
</Request>

我尝试了以下XPath查询,但徒劳无功:

//*[local-name()='name'][text()='Mobile_Number']/../*[local-name()='value']/text()

下面的查询会有效但后来又不是基于ID值Mobile_Number -

string(/*/*/*[local-name()='serviceCharacteristic']/*/*[local-name()='value'])

1 个答案:

答案 0 :(得分:2)

这个怎么样?

//serviceCharacteristic[specification/ID='Mobile_Number']/values/value/text()
相关问题