如何在 powershell 中通过 idref 查找 xml 节点
how to find xml node by idref in powershell
比如我有简单的xml
<A>
<networks type="A">
<hub id = "123">
<port>8080</port>
</hub>
<client idref = "123">
<host>some.value</host>
</client>
</networks>
</A>
<B>
<networks type="B">
<hub id = "235">
<port>9090</port>
</hub>
<client idref = "235">
<host>some.value</host>
</client>
</networks>
</B>
我使用具有特定类型的 foreach 循环遍历网络 items/nodes。
$networks = ($variable.SelectNodes('//networks') | Where-Object {$_.type -eq "$type*"})
foreach ($node in $networks) {
if($node.hub -ne $null) {
Write-Output 'hub:'
Write-Output 'id' $node.hub.id
Write-Output 'port' $node.hub.port
Write-Output '*********'
}
if($node.client -ne $null) {
Write-Output 'client:'
Write-Output 'idref:' $node.client.idref
Write-Output 'host:' $node.client.host
# how to pick port from networks//hub//port where hub.id = 123 by $node.client.idref here?
Write-Output '**********'
}
}
但是当当前节点是“client”时,我需要通过idref来区分端口和“hub”标签。
如何做到这一点?
我可能是误会了,但这行得通吗?
$xml = [xml] @"
<root>
<A>
<networks type="A">
<hub id = "123">
<port>8080</port>
</hub>
<client idref = "123">
<host>some.value</host>
</client>
</networks>
</A>
<B>
<networks type="B">
<hub id = "235">
<port>9090</port>
</hub>
<client idref = "235">
<host>some.value</host>
</client>
</networks>
</B>
</root>
"@
$networks = $xml.SelectNodes('//networks')
foreach ($node in $networks) {
if ($null -ne $node.hub) {
Write-Output 'hub:'
Write-Output 'id' $node.hub.id
Write-Output 'port' $node.hub.port
Write-Output '*********'
}
if ($null -ne $node.client) {
Write-Output 'client:'
Write-Output 'idref:' $node.client.idref
Write-Output 'host:' $node.client.host
# how to pick port from networks//hub//port where hub.id = 123 by $node.client.idref here?
Write-Output 'port: ' ($xml.SelectNodes('//networks') | Where-Object { $_.hub.id -eq $node.client.idref }).hub.port
Write-Output "**********"
}
}
比如我有简单的xml
<A>
<networks type="A">
<hub id = "123">
<port>8080</port>
</hub>
<client idref = "123">
<host>some.value</host>
</client>
</networks>
</A>
<B>
<networks type="B">
<hub id = "235">
<port>9090</port>
</hub>
<client idref = "235">
<host>some.value</host>
</client>
</networks>
</B>
我使用具有特定类型的 foreach 循环遍历网络 items/nodes。
$networks = ($variable.SelectNodes('//networks') | Where-Object {$_.type -eq "$type*"})
foreach ($node in $networks) {
if($node.hub -ne $null) {
Write-Output 'hub:'
Write-Output 'id' $node.hub.id
Write-Output 'port' $node.hub.port
Write-Output '*********'
}
if($node.client -ne $null) {
Write-Output 'client:'
Write-Output 'idref:' $node.client.idref
Write-Output 'host:' $node.client.host
# how to pick port from networks//hub//port where hub.id = 123 by $node.client.idref here?
Write-Output '**********'
}
}
但是当当前节点是“client”时,我需要通过idref来区分端口和“hub”标签。 如何做到这一点?
我可能是误会了,但这行得通吗?
$xml = [xml] @"
<root>
<A>
<networks type="A">
<hub id = "123">
<port>8080</port>
</hub>
<client idref = "123">
<host>some.value</host>
</client>
</networks>
</A>
<B>
<networks type="B">
<hub id = "235">
<port>9090</port>
</hub>
<client idref = "235">
<host>some.value</host>
</client>
</networks>
</B>
</root>
"@
$networks = $xml.SelectNodes('//networks')
foreach ($node in $networks) {
if ($null -ne $node.hub) {
Write-Output 'hub:'
Write-Output 'id' $node.hub.id
Write-Output 'port' $node.hub.port
Write-Output '*********'
}
if ($null -ne $node.client) {
Write-Output 'client:'
Write-Output 'idref:' $node.client.idref
Write-Output 'host:' $node.client.host
# how to pick port from networks//hub//port where hub.id = 123 by $node.client.idref here?
Write-Output 'port: ' ($xml.SelectNodes('//networks') | Where-Object { $_.hub.id -eq $node.client.idref }).hub.port
Write-Output "**********"
}
}