[英]how to find xml node by idref in powershell
for example, I have simple xml例如,我有简单的 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>
I iterate through network items/nodes using foreach loop with specific type.我使用具有特定类型的 foreach 循环遍历网络项/节点。
$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 '**********'
}
}
but when current node is "client", I need to distinguish port from "hub" tag by idref.但是当当前节点是“客户端”时,我需要通过 idref 将端口与“集线器”标签区分开来。 How to do this?
这个怎么做?
I might be misunderstanding, but does this work?我可能会误解,但这有效吗?
$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 "**********"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.