Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format of XPath Selec Statement

Status
Not open for further replies.

surge3333

Technical User
Sep 28, 2010
3
CA
I’m new to Xpath and having troubles extracting the values I want here. This is an excerpt of my XML file -

<?xml version="1.0"?>
<Response command="view" app="ci" version="4.10.0 2-1 5467">
<App-Connection port="1234" userID="fred" server="slate.com"></App-Connection>
<WorkItems selectionType="IssueIDToIssueSelection">
<WorkItem id="4321" displayId="4321" modelType="im.Issue">
<Field name="Type">
<Item id="Gravel" displayId="Gravel" modelType="im.Type">
</Item>
</Field>
<Field name="ID">
<Value dataType="int">4321</Value>
</Field>
<Field name="Summary">
<Value dataType="string">Bronty Burgers</Value>
</Field>
<Field name="State">
<Item id="Active" displayId="Active" modelType="im.State">
</Item>
</Field>
<Field name="Rubble">
<Value dataType="string">Hunger</Value>
</Field>
….



I can obtain the “State” value I’m after -

PS D:\> $xml.SelectNodes("//Field[@name='State']/Item/@id")

#text
-----
Active

However for the “Rubble” value, I’m getting the datatype returned as well as the value. Any idea how I can just get the value returned?


PS D:\> $xml.SelectNodes("//Field[@name='Rubble']/Value")

dataType #text
-------- -----
string Hunger


Thanks…


 
If you want to get the text node of Value node with parent as Field having the attribute name equal to Rubble in the interactive ps shell, you can do this:
[tt]
PS D:\> $xml.SelectNodes("//Field[@name='Rubble']/Value[red]/text()[/red]") | format-table value
[/tt]
Otherwise, to see its attribute dataType with correct xpath, it is this.
[tt]
PS D:\> $xml.SelectNodes("//Field[@name='Rubble']/Value[red]/@dataType[/red]") | format-table value
[/tt]
... not sure if it is what you have in mind!
 
Thanks tsuji,

I'm need to save the output here so I can pass it further down in my script. With your first example, I'm not able to get just the "Hunger" value stripped off -

$a = $xml.SelectNodes("//Field[@name='Rubble']/Value/text()")

$a.value??
I've tried everthing I can think of to pull the value off both the table a list with no luck.

The second example returns the Datatype/string info, I'm tring to get the #text/Hunger info.


Thanks
 
Since selectnodes return a nodelist, you call it via its item indexing, such as these.
[tt]
$a.Item(0).Value;
$a.Item(0).Data;
#etc etc
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top