2

I want to process following xml Using xpath query

<?xml version="1.0" encoding="UTF-8"?>
<UDSObjectList>
  <UDSObject>
    <Handle>chg:400106</Handle>
    <Attributes>
      <Attribute DataType="2002">
        <AttrName>chg_ref_num</AttrName>
        <AttrValue>123</AttrValue>
      </Attribute>
      <Attribute DataType="2002">
        <AttrName>requestor.combo_name</AttrName>
        <AttrValue>ServiceDesk, Administrator </AttrValue>
      </Attribute>
      <Attribute DataType="2005">
        <AttrName>status</AttrName>
        <AttrValue>AA</AttrValue>
      </Attribute>
      <Attribute DataType="2005">
        <AttrName>priority</AttrName>
        <AttrValue>3</AttrValue>
      </Attribute>
      <Attribute DataType="2002">
        <AttrName>log_agent.combo_name</AttrName>
        <AttrValue>ServiceDesk, Administrator </AttrValue>
      </Attribute>
      <Attribute DataType="2002">
        <AttrName>assignee.combo_name</AttrName>
        <AttrValue>Smith</AttrValue>
      </Attribute>
    </Attributes>
  </UDSObject>
</UDSObjectList>

I want to get the AttrValue of AttrName='chg_ref_num' using xpath query what will be the exact query.

I am trying using

"//UDSObject/Attributes/Attribute/AttrValue[AttrName=chg_ref_num]/text()" 

but not getting the AttrValue.

4

3 回答 3

0
"/UDSObjectList/UDSObject/Attributes/Attribute/AttrName[text()="chg_ref_num"]/parent::*/AttrValue/text()" 
于 2013-09-19T06:32:12.420 回答
0

您需要选择父节点才能获得正确的元素。因此,使用以下 xPath,您正在搜索一个 Attribute 元素,该元素有一个子节点,其文本节点具有搜索到的值。从该 Attribute 元素,您可以访问 AttrValue 节点的文本节点。

像这里这样:

//UDSObject/Attributes/Attribute[./AttrName/text()='chg_ref_num']/AttrValue/text()
于 2013-09-19T06:33:00.930 回答
0

尝试使用following-sibling

//AttrName[.='chg_ref_num']/following-sibling::AttrValue/text()

演示 - http://www.xpathtester.com/obj/c50bed92-850e-41b9-9a01-a5ecea6ead3e

于 2013-09-19T06:34:48.347 回答