如何使用 if 语句并从该语句的输出中删除子标签
xml 文件中的 if 语句从我的代码中解析了所有 xml 文件。
现在我想检查 sessionType 是否等于我的 $name
输入然后删除 int 标记之间的数据。
在不真正知道 XML 文件是什么样子的情况下,从您的代码来看,它应该如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Con>
<tar>
<tri>
<int>
<ses>Empty</ses>
<description>Empty session state</description>
<value>0</value>
</int>
</tri>
</tar>
<tar>
<tri>
<int>
<ses>RestrictedRemoteServer</ses>
<description>Restricted remote server</description>
<value>1</value>
</int>
</tri>
</tar>
<tar>
<tri>
<int>
<ses>Default</ses>
<description>Default session state</description>
<value>2</value>
</int>
</tri>
</tar>
</Con>
在这种情况下,这应该有效:
$name = Read-Host -Prompt "Enter the name of the sessiontype"
$result = Get-ChildItem -Path 'D:\Testing\TestcasesOutput' -Filter '*.xml' -File -Recurse | #'# get a collection of xml FileInfo objects
ForEach-Object {
$file = $_
try {
[xml]$xml = Get-Content -Path $file.FullName -ErrorAction Stop
# get an array of child nodes where the <ses> nodes contain the wanted sessionType
$nodes = @($xml.Con.tar.tri.int.ChildNodes | Where-Object { $_.'#text' -eq $name })
if ($nodes) {
# we have found <ses> nodes containing the wanted sessionType
[PSCustomObject]@{
'File' = $file.FullName
'SessionType' = $nodes[0].'#text'
}
# loop through the $nodes array
$nodes | ForEach-Object {
## DECIDE WHAT TO DO HERE ##
# remove all the subnodes of the <int> node, leaving an empty node <int></int>
$_.ParentNode.RemoveAll()
# OR: remove the <int> node completely, including all sub nodes
# $_.ParentNode.ParentNode.RemoveAll()
# OR: just remove the <ses>...</ses> subnode within the <int> node
# [void]$_.ParentNode.RemoveChild($_)
}
# create a filename for the output by adding 'Updated' to the name
$outputfile = Join-Path -Path $file.DirectoryName -ChildPath ('{0}_Updated{1}' -f $file.BaseName, $file.Extension)
# save the updated XML file
Write-Host "Saving file '$outputfile'"
$xml.Save($outputfile)
}
else {
Write-Host "File '$($file.FullName)' did not contain SessionType $name"
}
}
catch {
Write-Warning "Bad XML file found: '$($file.FullName)'"
}
}
# output on screen
$result
# or save the results as CSV file somewhere
# $result | Export-Csv -Path 'PATH TO THE RESULTS CSV FILE' -NoTypeInformation
Edit
如果你这样做NOT想要保留原始 XML 文件,但用更新的 xml 覆盖它,请使用以下命令:
$name = Read-Host -Prompt "Enter the name of the sessiontype"
$result = Get-ChildItem -Path 'D:\Testing\TestcasesOutput' -Filter '*.xml' -File -Recurse | #'# get a collection of xml FileInfo objects
ForEach-Object {
$file = $_.FullName
try {
[xml]$xml = Get-Content -Path $file -ErrorAction Stop
# get an array of child nodes where the <ses> nodes contain the wanted sessionType
$nodes = @($xml.Con.tar.tri.int.ChildNodes | Where-Object { $_.'#text' -eq $name })
if ($nodes) {
# we have found <ses> nodes containing the wanted sessionType
[PSCustomObject]@{
'File' = $file
'SessionType' = $nodes[0].'#text'
}
# loop through the $nodes array
$nodes | ForEach-Object {
## DECIDE WHAT TO DO HERE ##
# remove all the subnodes of the <int> node, leaving an empty node <int></int>
$_.ParentNode.RemoveAll()
# OR: remove the <int> node completely, including all sub nodes
# $_.ParentNode.ParentNode.RemoveAll()
# OR: just remove the <ses>...</ses> subnode within the <int> node
# [void]$_.ParentNode.RemoveChild($_)
}
# save the updated XML file
Write-Host "Saving file '$file'"
$xml.Save($file)
}
else {
Write-Host "File '$file' did not contain SessionType $name"
}
}
catch {
Write-Warning "Bad XML file found: '$file'"
}
}
# output on screen
$result
# or save the results as CSV file somewhere
# $result | Export-Csv -Path 'PATH TO THE RESULTS CSV FILE' -NoTypeInformation
当与用户输入一起使用时Empty
,结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<Con>
<tar>
<tri>
<int>
</int>
</tri>
</tar>
<tar>
<tri>
<int>
<ses>RestrictedRemoteServer</ses>
<description>Restricted remote server</description>
<value>1</value>
</int>
</tri>
</tar>
<tar>
<tri>
<int>
<ses>Default</ses>
<description>Default session state</description>
<value>2</value>
</int>
</tri>
</tar>
</Con>
希望有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)