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!

powershell, xpathdocument, getattribute xpath 1

Status
Not open for further replies.

mrmovie

Technical User
Oct 2, 2002
3,094
GB
$xPathDoc = new-object -typename System.Xml.XPath.XPathDocument -ArgumentList "c:\programdata\gws\buildinfo.xml"
$nav = $xPathDoc.CreateNavigator()
$strXPath = "GWS/SETUP@version"
$result = $nav.GetAttribute($strXPath, "")

so, i am trying to grab the attribute of SETUP node which is part of the GWS node.
Is what i am attempting possible? i want to use a simple xpath statement to locate the attribute.
the only examples i can find so far have been using the
$nav.Select("GWS/SETUP") then $nav.GetAttribute("version")

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Can you try what you get? The xpath below should be a correct one, depend on your exact xml document. It is proper for a structure like this.
[tt]
<GWS>
<SETUP version="1.0">
<!-- etc etc -->
</SETUP>
<!-- can even have more SETUP -->
</GWS>
[/tt][tt]
#... etc etc
$strXPath = "/GWS/SETUP/@version"
$result = $nav.Select($strXPath)

$result | Format-Table -property Value
[/tt]
 
i could kiss you tsuji,
I am getting

Value
-----
1.0.3.5

I think i was missing a / between the node name and the attribute.

Can i follow up with how do i get the .Value of it?
i was hoping $result = "1.0.3.5" as string a string. $result.ToString() gives me MS.Internal.Xml.XPath.XPathSelectionIterator

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
$result.MoveNext()
write-host $result.Current.Value
:)

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
All good :)

While there might be more than one item return, can do this for inspection.
[tt]
while ($result.MoveNext()){
Write-Host $result.Current.Value
}
[/tt]
 
thanks tsuji, i tested having more than one node with that attribute, with the code you describe. I was going to say i would leave that bit of the code out as the purpose of searching is to return only one value, and this would be the first one the $result contained. however, thinking about it i cant decide if i should just return a delimited list to the calling process.

thanks again for your input

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
That's my understanding too. What left is the guard against xpath finding none. Again, that's for general consideration only, not for your concrete case.
[tt]
if ($result.MoveNext()) {
$ver=$result.Current.Value
} else {
#whatever to do on $var
}
[/tt]
 
sorry tsuji, i cant give you more than one star per thread. will test "If ($result.MoveNext())" should give me my check for not finding anything

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top