Xml Powershell可从文件中删除特定文本
我真的不知道如何最好地处理这个问题。 我想替换xml.rels文件中的文本 它们包含xml数据,但似乎不喜欢用$xml=新对象xml打开 我想删除目标标记中的所有路径信息 一个文件示例是Xml Powershell可从文件中删除特定文本,xml,powershell,Xml,Powershell,我真的不知道如何最好地处理这个问题。 我想替换xml.rels文件中的文本 它们包含xml数据,但似乎不喜欢用$xml=新对象xml打开 我想删除目标标记中的所有路径信息 一个文件示例是 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath" Target="/Finance/Audit/Mgt%20Accts%202020/MGT%20ACCTS%20-%20Copy2/Mgt%20Accts%20MAR%2020.xlsx" TargetMode="External"/></Relationships>
因此,在这种情况下,我希望该文件成为
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath" Target="Mgt%20Accts%20MAR%2020.xlsx" TargetMode="External"/></Relationships>
现在目标可以包含任何路径,因此我需要找到目标中的最后一个“/”并在其后保留文本
谢谢你的帮助。
P
您可以通过加载XML文件、循环元素,然后使用
SetAttribute()
方法更改目标属性来完成此操作:
$sourceFolder = "Y:\Finance\Audit\Mgt Accts 2020\MGT ACCTS - Copy3\Mgt Accts APR 20 unzipped\xl\externalLinks_rels"
# PowerShell versions below 3.0 need to use:
# Get-ChildItem -Path $sourceFolder -Filter '*.xml' | Where-Object { !$_.PSIsContainer } | Foreach-Object {
Get-ChildItem -Path $sourceFolder -Filter '*.xml' -File | Foreach-Object {
# load the xml file
[xml]$xml = Get-Content $_.FullName -Raw
# loop through the <RelationShip> elements and change the Target attribute
foreach ($relationNode in $xml.Relationships.Relationship) {
# or use $newTarget = Split-Path -Path $relationNode.Target -Leaf
$newTarget = [System.IO.Path]::GetFileName($relationNode.Target)
$relationNode.SetAttribute("Target", $newTarget)
}
$xml.Save($_.FullName)
}
$sourceFolder=“Y:\Finance\Audit\Mgt acts 2020\Mgt acts-Copy3\Mgt acts APR 20 unzip\xl\externalLinks\u rels”
#3.0以下的PowerShell版本需要使用:
#Get-ChildItem-Path$sourceFolder-Filter'*.xml'| Where对象{!$\uz.PSIsContainer}| Foreach对象{
Get-ChildItem-Path$sourceFolder-Filter'*.xml'-File | Foreach对象{
#加载xml文件
[xml]$xml=Get Content$\.FullName-Raw
#循环遍历元素并更改目标属性
foreach($xml.relations.Relationship中的relationNode){
#或者使用$newTarget=Split Path-Path$relationNode.Target-Leaf
$newTarget=[System.IO.Path]::GetFileName($relationNode.Target)
$relationNode.SetAttribute(“目标”,“新目标”)
}
$xml.Save($\ xml.FullName)
}
请添加到目前为止您尝试的代码。Get-ChildItem-Path“Y:\Finance\Audit\Mgt Accts 2020\Mgt Accts-Copy3\Mgt Accts APR 20 unzip\xl\externalLinks\u rels\””Foreach对象{#$content=Get content$\全名写入主机$\全名$xml=New Object xml$xml.Load($\全名)$nodes xml.SelectNodes(“/Relationships”);foreach($nodes中的节点){write host$node.GetAttribute(“Target”)}您好@Alex\P我无法让它说出我的代码的目标属性中有什么,更不用说更改了!:@pwhelan您正在对-Filter'*.xml.rels'
进行筛选。您的xml文件真的是这样重命名的吗?现在,get ChildItem
找不到任何内容,因此出现了错误。另外,您确定$relsFolder
中的路径正确吗?(指向文件夹,而不是文件)。另外,您使用的PowerShell版本是什么?3.0以下的版本没有-FILE
开关,因此,对于这些版本,您需要使用| Where Object{!$.PSIsContainer}ForEach Object{..