techie11
techie11

Reputation: 1387

Why this xpath doesn't work?

given docuent a.xml:

<?xml version="1.0"?>
<xml>
  <listUsersResponse xmlns="http://www.algorithmics.com/schema">
    <status>OK</status>
    <users size="615">
      <user>
        <id>user1</id>
        <name>Joe Doe</name>
        <attributes size="0"/>
        <status>ACTIVE</status>
        <roleId>CREDIT_SUPPORT</roleId>
        <password>454E454A77484D3566717547686858726842503755513D3D</password>
        <timeout>0</timeout>
      </user>
      <user>
        <id>user2</id>
        <name>Tom Smith</name>
        <attributes size="0"/>
        <status>ACTIVE</status>
        <roleId>RISK_MANAGEMENT</roleId>
        <password>627678416458513567624E37384C314E626C30672B773D3D</password>
        <timeout>0</timeout>
      </user>
    </users>
  </listUsersResponse>
</xml>

I want to extract the xml of the <user> node which has a id element with value "user1".

@skovorodkin: thank you for the tip. now I get the node. but it doesn't show the full contents of the node:

$  xmllint --shell a.xml <<EOF
...setns x=http://www.algorithmics.com/schema
...xpath //x:users/x:user[x:id="user1"]
...EOF
/ > / > Object is a Node Set :
Set contains 1 nodes:
1  ELEMENT user

I expect:

<user>
        <id>user1</id>
        <name>Joe Doe</name>
        <attributes size="0"/>
        <status>ACTIVE</status>
        <roleId>CREDIT_SUPPORT</roleId>
        <password>454E454A77484D3566717547686858726842503755513D3D</password>
        <timeout>0</timeout>
 </user>

Upvotes: 0

Views: 1980

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

The XPath is working correctly and selecting the element you are looking for. You have an issue displaying the node that the XPath expression found. That's going to depend on the application or tool that you use to process the results of the XPath, not on the XPath itself.

Upvotes: 2

Related Questions