将迭代 SOAP XML 行转置为 PowerShell 中的列
Transpose iterative SOAP XML rows into columns in PowerShell
在 的后续 post 中,我希望能够从 result
项目集中迭代键值对,如下所示:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<deviceListResponse xmlns="http://SERVERURL.domain/">
<!-- Item 1 -->
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-123ABC456</second>
<key>device.longname</key>
<value>DESKTOP-123ABC456</value>
</items>
</return>
<!-- Item 2 -->
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-Bus</second>
<key>device.longname</key>
<value>DESKTOP-Bus</value>
</items>
</return>
</deviceListResponse>
</soap:Body>
</soap:Envelope>
如何在 PowerShell 脚本中实现此目的?提前致谢。
只需要对 :
进行相对较小的调整
# Assume that $xmlText contains the XML text to parse.
# Initialize the ordered hashtable that will collect the key-value pairs
# from the XML.
$oht = [ordered] @{}
Select-Xml -Content $xmlText //ns:items -Namespace @{ ns = 'http://SERVERURL.domain/'} |
ForEach-Object {
# See if an entry already exists for the key at hand.
if ($oht.Contains($_.Node.key)) {
# Convert the entry value into an array on demand and
# append the value at hand.
[array] $oht[$_.Node.key] += $_.Node.value
}
else {
# Create the entry, using the value as-is.
$oht[$_.Node.key] = $_.Node.value
}
}
$customObj = [pscustomobject] $oht
$customObj # output
以上在 $customObj
中产生以下 [pscustomobject]
实例:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
{123456789, 123456789} {127.0.0.1, 127.0.0.1} {DESKTOP-123ABC456, DESKTOP-Bus}
请注意,{ ... }
附件只是 PowerShell 输出格式化系统(可能令人困惑)表示值是 集合(数组)的方式。
在 result
项目集中迭代键值对,如下所示:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<deviceListResponse xmlns="http://SERVERURL.domain/">
<!-- Item 1 -->
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-123ABC456</second>
<key>device.longname</key>
<value>DESKTOP-123ABC456</value>
</items>
</return>
<!-- Item 2 -->
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-Bus</second>
<key>device.longname</key>
<value>DESKTOP-Bus</value>
</items>
</return>
</deviceListResponse>
</soap:Body>
</soap:Envelope>
如何在 PowerShell 脚本中实现此目的?提前致谢。
只需要对
# Assume that $xmlText contains the XML text to parse.
# Initialize the ordered hashtable that will collect the key-value pairs
# from the XML.
$oht = [ordered] @{}
Select-Xml -Content $xmlText //ns:items -Namespace @{ ns = 'http://SERVERURL.domain/'} |
ForEach-Object {
# See if an entry already exists for the key at hand.
if ($oht.Contains($_.Node.key)) {
# Convert the entry value into an array on demand and
# append the value at hand.
[array] $oht[$_.Node.key] += $_.Node.value
}
else {
# Create the entry, using the value as-is.
$oht[$_.Node.key] = $_.Node.value
}
}
$customObj = [pscustomobject] $oht
$customObj # output
以上在 $customObj
中产生以下 [pscustomobject]
实例:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
{123456789, 123456789} {127.0.0.1, 127.0.0.1} {DESKTOP-123ABC456, DESKTOP-Bus}
请注意,{ ... }
附件只是 PowerShell 输出格式化系统(可能令人困惑)表示值是 集合(数组)的方式。