基于节点值获取节点名称

huangapple go评论57阅读模式
英文:

Get node names based on node values

问题

我有以下的XML

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration APIVersion="1905.1" IPS_CAT_VER="0">
        <Notificationlist transactionid="">
            <SendEmail>Enable</SendEmail>
            <SendSnmp>Disable</SendSnmp>
            <SignInEmail>Enable</SignInEmail>
            <SignInSnmp>Disable</SignInSnmp>
            <TooManyLoginEmail>Disable</TooManyLoginEmail>
            <TooManyLoginSnmp>Disable</TooManyLoginSnmp>
        </Notificationlist>
    </Configuration>

我需要两个数组,一个包含**Enable**的Notificationlist,另一个包含**Disable**的Notificationlist,像这样

    Enable        Disable
    ------        -------
    SendEmail     SendSnmp
    SignInEmail   SignInSnmp
                  TooManyLoginEmail
                  TooManyLoginSnmp

我尝试过如下:

    $xml = (Select-Xml -Path C:\Users\Lenovo\Desktop\shellscripts\xml\SNMP.xml -XPath '/Configuration/Notificationlist')
    $nodes = $xml | ForEach-Object { $_.Node | select -ExpandProperty InnerText }

但它只提供节点的值,而不是节点名称。
英文:

I have following XML

<?xml version="1.0" encoding="UTF-8"?>
<Configuration APIVersion="1905.1" IPS_CAT_VER="0">
    <Notificationlist transactionid="">
        <SendEmail>Enable</SendEmail>
        <SendSnmp>Disable</SendSnmp>
        <SignInEmail>Enable</SignInEmail>
        <SignInSnmp>Disable</SignInSnmp>
        <TooManyLoginEmail>Disable</TooManyLoginEmail>
        <TooManyLoginSnmp>Disable</TooManyLoginSnmp>
    </Notificationlist>
</Configuration>

I need two arrays, one with Enable Notificationlist and one with Disable Notificationlist like

Enable        Disable
------        -------
SendEmail     SendSnmp
SignInEmail   SignInSnmp
              TooManyLoginEmail
              TooManyLoginSnmp

I have tried like

$xml = (Select-Xml -Path C:\Users\Lenovo\Desktop\shellscripts\xml\SNMP.xml -XPath '/Configuration/Notificationlist')
$nodes = $xml | ForEach-Object { $_.Node | select -ExpandProperty InnerText }

but it only gives node values but not node name.

答案1

得分: 1

我建议加载XML一次,然后执行两个XPath查询。

$xml = [xml]::new(); $xml.Load("C:\Users\Lenovo\Desktop\shellscripts\xml\SNMP.xml")

$enableNames = $xml | Select-Xml -XPath '/Configuration/Notificationlist/*[text()="Enable"]' | 
               ForEach-Object { $_.Node.Name }

$disableNames = $xml | Select-Xml -XPath '/Configuration/Notificationlist/*[text()="Disable"]' | 
                ForEach-Object { $_.Node.Name }

XPath表达式 *[text()="SomeText"] 将返回所有具有给定文本的文本子节点的元素。

要获取节点名称,只需查询节点的 Name 属性。

英文:

As you want to do two distinct queries I suggest to load the XML once and then do two XPath queries.

$xml = [xml]::new(); $xml.Load("C:\Users\Lenovo\Desktop\shellscripts\xml\SNMP.xml")

$enableNames = $xml | Select-Xml -XPath '/Configuration/Notificationlist/*[text()="Enable"]' | 
               ForEach-Object { $_.Node.Name }

$disableNames = $xml | Select-Xml -XPath '/Configuration/Notificationlist/*[text()="Disable"]' | 
                ForEach-Object { $_.Node.Name }

The XPath expression *[text()="SomeText"] gives you all elements that have text child nodes with the given text.

To get the node name simply query the Name property of the node.

答案2

得分: 1

你可以使用以下代码,它将为你提供两个变量 $Enabled$Disabled。这将为你提供已启用和已禁用值的节点名称的两个数组:

$Enabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Enable") { $_ } }

$Disabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Disable") { $_ } }
英文:

You can use below code, which will give you two variables $Enabled and $Disabled. To give you two arrays for node names for enabled and disabled values:

$Enabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Enable") { $_ } }

$Disabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Disable") { $_ } }

huangapple
  • 本文由 发表于 2023年5月22日 22:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定