首页 > 解决方案 > ForEach 列表的每一行获取字符串,在 XML 中搜索特定节点并使用 Powershell 删除父节点

问题描述

我有一个字符串列表,每行都有一个 XML 文件中的对应节点。需要删除该 XML 中的所有父级。该脚本大致工作,但结果不正确。而且我在$lineXpath 查询中的变量出现了一些错误。我正在努力弄清楚该变量发生了什么。

romlist.txt:

1941.zip
llander.zip
warrior.zip

游戏列表.xml:

<?xml version="1.0" encoding="UTF-8"?>
  <gameList>
      <game id="46415" source="screenscraper.fr">
          <path>.aaa/bbb/warrior.zip</path>
          <name>Warrior</name>
          <desc>qweqwe</desc>
          <image>./images/warrior-image.png</image>
          <rating>0.7</rating>
          <releasedate>19790101T000000</releasedate>
          <developer>Tim Skelly</developer>
          <publisher>Vectorbeam</publisher>
          <genre>Fight / N/A</genre>
          <players>2</players>
          <video>./videos/warrior-video.mp4</video>
      </game>
      <game id="39873" source="screenscraper.fr">
          <path>.ccc/ddd/1941.zip</path>
          <name>1941 - Counter Attack</name>
          <desc>asdasd</desc>
          <image>./images/1941-image.png</image>
          <rating>0.8</rating>
          <releasedate>19900101T000000</releasedate>
          <developer>Capcom</developer>
          <publisher>Capcom</publisher>
          <genre>Shoot'em up / Vertical / Shoot'em Up</genre>
          <players>2</players>
          <video>./videos/1941-video.mp4</video>
      </game>
      <game id="46408" source="screenscraper.fr">
          <path>.eee/fff/sundance.zip</path>
          <name>Sundance</name>
          <desc>zxczxc</desc>
          <image>./images/sundance-image.png</image>
          <rating>0.5</rating>
          <releasedate>19790101T000000</releasedate>
          <developer>Tim Skelly</developer>
          <publisher>Cinematronics</publisher>
          <genre>N/A / Various</genre>
          <players>2</players>
          <video>./videos/sundance-video.mp4</video>
      </game>
      <game id="46393" source="screenscraper.fr">
          <path>.ggg/hhh/llander.zip</path>
          <name>Lunar Lander</name>
          <desc>asdasd</desc>
          <image>./images/llander-image.png</image>
          <rating>0.9</rating>
          <releasedate>19790101T000000</releasedate>
          <developer>Atari</developer>
          <publisher>Atari</publisher>
          <genre>Various / N/A / Simulation</genre>
          <players>1</players>
          <video>./videos/llander-video.mp4</video>
      </game>
  </gameList>

电源外壳:

$input = "gamelist.xml"
$output = "gamelist_result.xml"
$romlist = "romlist.txt"

[xml]$xml = Get-Content -Path $input -Raw

ForEach($line in Get-Content $romlist) {
    $rom = $xml.selectSingleNode('//game/path[contains(text(),line)]') | Select-Object -ExpandProperty "#text"
    ForEach($node in $xml.gameList.game | Where-Object {$_.path -eq $rom})
    {
    $xml.gameList.RemoveChild($node)
    }
}

$path = (join-path $pwd $output)
$xml.Save($path)

如果$xml.selectSingleNode('//game/path[contains(text(),line)]')我们没有错误但结果是错误的。

如果$xml.selectSingleNode('//game/path[contains(text(),$line)]')我们有一些错误并且结果被反转,则列表将被保留而不是被删除。

我将不胜感激解决此问题的任何帮助

标签: xmllistpowershellxpathforeach

解决方案


如果我正确理解你。

$rom, $inp = 'C:\path\romlist.txt', 'C:\path\gamelist.xml'
($lst, $xml = (Get-Content $rom), [xml](Get-Content $inp))[0].ForEach{
   $nod = $xml.SelectSingleNode("//path[contains(text(), '$_')]")
   [void]$xml.gameList.RemoveChild($nod.ParentNode)
}
$xml.Save('C:\path\gamelist_result.xml')

推荐阅读