gpt4 book ai didi

xml - 电源外壳 : sort xml by elements by child node value

转载 作者:数据小太阳 更新时间:2023-10-29 02:26:29 24 4
gpt4 key购买 nike

如何使用 Powershell 按此“元组”列表(键入的“整数”)的第二个节点对此 xml 进行降序排序。

所以结果将是 xml,但第一个元素将是 COMPUTERSF34,然后是 COMPUTER123..

   <Result>
<Tuple>
<Answer type="string">COMPUTERSF34</Answer>
<Answer type="integer">93</Answer>
</Tuple>
<Tuple>
<Answer type="string">NYCCOMPUTER01</Answer>
<Answer type="integer">44</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER_05</Answer>
<Answer type="integer">45</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER56</Answer>
<Answer type="integer">38</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER123</Answer>
<Answer type="integer">51</Answer>
</Tuple>
...
</Result>

最佳答案

试试这个:

$xml = [xml]@'
<Result>
<Tuple>
<Answer type="string">COMPUTERSF34</Answer>
<Answer type="integer">93</Answer>
</Tuple>
<Tuple>
<Answer type="string">NYCCOMPUTER01</Answer>
<Answer type="integer">44</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER_05</Answer>
<Answer type="integer">45</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER56</Answer>
<Answer type="integer">38</Answer>
</Tuple>
<Tuple>
<Answer type="string">COMPUTER123</Answer>
<Answer type="integer">51</Answer>
</Tuple>
</Result>
'@
$sorted = $xml.Result.Tuple | sort {[int]$_.Answer[1].'#text'} -desc
$lastChild = $sorted[-1]
$sorted[0..($sorted.Length-2)] | Foreach {$xml.Result.InsertBefore($_,$lastChild)}
$xml.Save('c:\foo.xml')

诀窍是获取每个已排序的节点(最后一个节点除外)并将其插入到新的(排序后)最后一个节点之前。

关于xml - 电源外壳 : sort xml by elements by child node value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24746205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com