【发布时间】:2015-07-18 04:01:45
【问题描述】:
Powershell: How to use Format-Table with XML data 详细说明了如何处理带有属性的 XML 元素。我的情况与此不同。
在大多数情况下,不存在任何属性:
<content type="application/xml">
<m:properties>
<d:Title>Vivamus fermentum semper porta</d:Title>
可以用这段代码处理:
...
$properties = $xml.feed.entry.content.properties
$properties | Format-Table -Property @{Label="Title"; Expression={$_.Title}}
...
甚至:
$properties | Format-Table -Property Title
然而,在少数情况下,xml:space 属性存在:
<content type="application/xml">
<m:properties>
<d:Title xml:space="preserve">Lorem ipsum dolor sit amet </d:Title>
需要此代码:
...
$properties = $xml.feed.entry.content.properties
$properties | Format-Table -Property @{Label="Title"; Expression={$_.Title."#text"}}
...
有没有 DRYer 方法来处理这种情况?
【问题讨论】:
-
我想我有一个想法,但您能否包含一个样本,该样本具有您所指的两种差异进行测试。由于您的第二个 sn-p 看起来 非常 与第一个相似。
-
我更新了我的问题,@Matt。
标签: xml powershell powershell-3.0