.net - XPath in PowerShell -


i converting ruby scripts posh:

> gem install nokogiri  > irb  > require 'nokogiri'  > $html = nokogiri::html("<div><img src='//127.0.0.1:5598/user/first.png' />                        <img src='//127.0.0.1:5598/user/second.png' /></div>")  > $html.xpath('//img[contains(@src,"first")]')  # output: <img src='//127.0.0.1:5598/user/first.png' /> 

in powershell, have:

> [system.reflection.assembly]::loadwithpartialname("system.xml.linq")  > [system.reflection.assembly]::loadwithpartialname("system.xml.xpath")  > $html = [system.xml.linq.xdocument]::parse("<div>                        <img src='//127.0.0.1:5598/user/first.png' />                        <img src='//127.0.0.1:5598/user/second.png' /></div>")  > [system.xml.xpath.extensions]::xpathselectelement($html,                                    '//img[contains(@src,"first")]')  # displays properties of xelement type object 

how same output?

is there better way parsing html in powershell v.4?

just add .tostring() , same output.

here simpler alternative produces same:

$html = [xml] "<div><img src='//127.0.0.1:5598/user/first.png' />                     <img src='//127.0.0.1:5598/user/second.png' /></div>" $html.selectsinglenode('//img[contains(@src,"first")]').outerxml 

or even

($html.div.img | ?{ $_.src -match 'first' }).outerxml 

note assuming dealing xml per own powershell example (i not used handling html)…


Comments