我有一个带有字符串的列表,其中每一行在XML文件中都有一个对应的节点。所有父级都需要在该XML中删除。
该脚本可以正常运行,但结果不正确。而且我在Xpath查询中的变量$line
遇到了一些错误。
我正在努力弄清楚该变量的情况。
romlist.txt:
1941.zip
llander.zip
warrior.zip
gamelist.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>
POWERSHELL:
$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)]')
出现错误,并且结果反转,则列表将保留而不是删除。
希望能帮助您解决此问题
答案 0 :(得分:0)
如果我正确理解你的话。
$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')
答案 1 :(得分:0)
您的代码几乎是正确的,但是这一行存在问题:
$xml.selectSingleNode('//game/path[contains(text(),line)]')
如果您希望Powershell将line
替换为其值,请使用外部双引号和$
。还需要在$line
周围加上文字单引号,因为函数contains
希望它们用于定界值。
$xml.selectSingleNode("//game/path[contains(text(),'$line')]")
有了该更改,它应该可以工作。但是,我对其余部分进行了重构,以使其更加简洁。
ForEach($line in Get-Content $romlist) {
$rom = $xml.selectSingleNode("//game/path[contains(text(),'$line')]") | Select-Object -ExpandProperty "#text"
$xml.gameList.ChildNodes | Where-Object{ $_.path -eq $rom } | ForEach-Object { $xml.gameList.RemoveChild($_) }
}