2

I am converting some ruby scripts to 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, I 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")]')

# It displays the properties of XElement type object

How to get the same output?

Is there a better way parsing html in PowerShell v.4?

3 Answers 3

2

Just add .ToString() and you will get the same output.

Here is a simpler alternative which produces the 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 that I am assuming you are dealing with XML as per your own PowerShell example (I am not used to handling HTML)…

Sign up to request clarification or add additional context in comments.

Comments

2

Another way for query XML just using CMDLET:

$xml = [xml]@"
<div>
<img src='//127.0.0.1:5598/user/first.png' />
<img src='//127.0.0.1:5598/user/second.png' />
</div>
"@

(select-xml -xml $xml -xpath '//img[contains(@src,"first")]' ) | % { $_.node.src }

Comments

1

another alternative using invoke-webrequest (PS V3) :

$ie = new-object -com "InternetExplorer.Application"
$ie.Navigate("c:\temp\test.html")
$html=$ie.Document
$html.images|% { if ($_.src -match "first") {echo $_.outerHTML}}

note that if its not a local file you can use :

 $html = Invoke-WebRequest "http://yourURL"

then parse $html.ParsedHtml.body

1 Comment

Good thinking, nice approach! Don't forget to close IE, though :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.