我正在使用Powershell的Microsoft服務器。 我要做的任務是獲取並設置"測試用例"類型的給定工作項的"步驟"。
,TFS在XML文檔中存儲諸如 HTML?HTML這樣的信息,以避免HTML元素,從而避免使用 XML 。
下面是壹個示例:
復制代碼
<steps id="0" last="3">
<step id="2" type="ValidateStep">
<parameterizedString isformatted="true">
<DIV><P>I do this and that</P></DIV> </parameterizedString>
<parameterizedString isformatted="true">
<DIV><P>&nbsp;</P></DIV> </parameterizedString>
<description/>
</step>
<step id="3" type="ActionStep">
<parameterizedString isformatted="true">
<DIV><DIV><P>I do something else <BR/></P></DIV></DIV> </parameterizedString>
<parameterizedString isformatted="true">
<DIV><P>This happens <BR/></P></DIV> </parameterizedString>
<description/>
</step></steps>
顯示為:?
如何獲得每個項目的"純文本"? 比如?This happens?離開了?<DIV><P>This happens <BR/></P></DIV>?我必須編寫自己的解析器,還是已經經有壹些可以以使用的解析器?
html
powershell
tfs
時間:17年09月18日原作者:jir?***1個回答
0?0
在?System.Web?命名空間中有壹些可以幫助妳的東西:
復制代碼
PS> add-type -AssemblyName system.webPS> [System.Web.HttpUtility]::HtmlDecode("Baskin & Robbins")Baskin & Robbins
更新
我又讀了妳的問題,妳想要的不止這個。 如果妳不熟悉xml和html語義,那麽這是壹個有點復雜的問題,因這裏這是壹個腳本。 我希望妳能根據需要修改它。
復制代碼
add-type -AssemblyName system.web$raw = @'
<steps id="0" last="3">
<step id="2" type="ValidateStep">
<parameterizedString isformatted="true">
<DIV><P>I do this and that</P></DIV>
</parameterizedString>
<parameterizedString isformatted="true">
<DIV><P>&nbsp;</P></DIV>
</parameterizedString>
<description/>
</step>
<step id="3" type="ActionStep">
<parameterizedString isformatted="true">
<DIV><DIV><P>I do something else <BR/></P></DIV></DIV>
</parameterizedString>
<parameterizedString isformatted="true">
<DIV><P>This happens <BR/></P></DIV>
</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'"
}}