當前位置:編程學習大全網 - 源碼下載 - 請問Powershell解析html的問題

請問Powershell解析html的問題

我正在使用Powershell的Microsoft服務器。 我要做的任務是獲取並設置"測試用例"類型的給定工作項的"步驟"。

,TFS在XML文檔中存儲諸如 HTML?HTML這樣的信息,以避免HTML元素,從而避免使用 XML 。

下面是壹個示例:

復制代碼

<steps id="0" last="3">

<step id="2" type="ValidateStep">

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;I do this and that&lt;/P&gt;&lt;/DIV&gt; </parameterizedString>

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt; </parameterizedString>

<description/>

</step>

<step id="3" type="ActionStep">

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;I do something else &lt;BR/&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt; </parameterizedString>

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;This happens &lt;BR/&gt;&lt;/P&gt;&lt;/DIV&gt; </parameterizedString>

<description/>

</step></steps>

顯示為:?

如何獲得每個項目的"純文本"? 比如?This happens?離開了?&lt;DIV&gt;&lt;P&gt;This happens &lt;BR/&gt;&lt;/P&gt;&lt;/DIV&gt;?我必須編寫自己的解析器,還是已經經有壹些可以以使用的解析器?

html

powershell

tfs

時間:17年09月18日原作者:jir?***1個回答

0?0

在?System.Web?命名空間中有壹些可以幫助妳的東西:

復制代碼

PS> add-type -AssemblyName system.web

PS> [System.Web.HttpUtility]::HtmlDecode("Baskin &amp; Robbins")Baskin & Robbins

更新

我又讀了妳的問題,妳想要的不止這個。 如果妳不熟悉xml和html語義,那麽這是壹個有點復雜的問題,因這裏這是壹個腳本。 我希望妳能根據需要修改它。

復制代碼

add-type -AssemblyName system.web

$raw = @'

<steps id="0" last="3">

<step id="2" type="ValidateStep">

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;I do this and that&lt;/P&gt;&lt;/DIV&gt;

</parameterizedString>

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;

</parameterizedString>

<description/>

</step>

<step id="3" type="ActionStep">

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;I do something else &lt;BR/&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;

</parameterizedString>

<parameterizedString isformatted="true">

&lt;DIV&gt;&lt;P&gt;This happens &lt;BR/&gt;&lt;/P&gt;&lt;/DIV&gt;

</parameterizedString>

<description/>

</step>

</steps>

'@$xml = [xml]$raw

$xml.steps.step | foreach-object {

write-host ('Processing {0}...' -f $_.type)

$_.parameterizedString | foreach-object {

# decode html entities

$html = [System.Web.HttpUtility]::HtmlDecode($_.innerText)

# let's hope the html is balanced and valid xhtml (matching begin/end tags)

# assumption is that the innermost <P> wraps the desired text

# match with xpath

$text = ([xml]$html).SelectSingleNode('//P/text()').value

write-host"Text: '$text'"

}}

  • 上一篇:主頁插入透明FLASH代碼?
  • 下一篇:哪個接口源碼比較好?
  • copyright 2024編程學習大全網