【发布时间】:2019-01-01 21:57:31
【问题描述】:
我有两个 XML 文件,一个具有默认名称和值(名为 Test.xml),另一个仅具有默认名称(名为 document.xml)。目标是用值替换默认名称 - 但仅在第一次出现时。
这里是Test.xml:
<XML-TEST>
<MyText>Dies ist ein Test</MyText>
<MyTexttwo>Dies ist noch ein Test</MyTexttwo>
</XML-TEST>
这是document.xml(几乎在最后):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
mc:Ignorable="w14 w15 w16se wp14">
<w:body>
<w:p w:rsidR="00E64ECE" w:rsidRDefault="00E64ECE" w:rsidP="00E64ECE">
<w:proofErr w:type="spellStart" />
<w:r>
<w:t>MyText</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
</w:p>
<w:p w:rsidR="00D50239" w:rsidRPr="00E64ECE" w:rsidRDefault="00E64ECE" w:rsidP="00E64ECE">
<w:r>
<w:t>MyTexttwo</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack" />
<w:bookmarkEnd w:id="0" />
</w:p>
<w:sectPr w:rsidR="00D50239" w:rsidRPr="00E64ECE">
<w:pgSz w:w="11906" w:h="16838" />
<w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />
<w:cols w:space="708" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:body>
</w:document>
我在用 PowerShell 做什么?
-
我将
PS> $XMLSourceHashtable 名称 值 ---- ----- MyText 在测试中消亡 MyTexttwo Dies ist noch ein 测试Test.xml(有值的那个)保存在哈希表中: 将
document.xml保存到变量$DocumentXml中。-
使用
foreach替换我需要的:foreach ($key in ($XMLSourceHashtable.GetEnumerator())) { # If one key.value is "false" replace the 1:1 name with Char if ($key | Where-Object {$_.Value -eq "false"}) { #$key.Name.Trim() #$DocumentXml.InnerXml = $DocumentXml.InnerXml.Replace($key.Name.Trim(), "â˜") } elseif ($key | Where-Object {$_.Value -eq "true"}) { # If one key.value is "true" replace the 1:1 name with Char #$key.Name.Trim() #$DocumentXml.InnerXml = $DocumentXml.InnerXml.Replace($key.Name.Trim(), "☒") } else { # Everything else needs to be replaced by value in hashtable #Write-Host $key.Name.Trim() "--------------" $key.Value.Trim() #$DocumentXml.InnerXml = $DocumentXml.InnerXml.Replace($key.Name.Trim(), $key.Value.Trim()) } }
前两个elseif 工作正常,不应考虑它们。我担心的是else。
会发生什么?
文本当然会替换,但替换方法会执行以下操作:
document.xml 中的值被替换如下:
“MyText”→“Dies ist ein Test”
"MyTexttwo" → 死在 Testtwo"
但应该是:
“MyText”→“Dies ist ein Test”
“MyTexttwo”→ Dies ist noch ein Test”
重点是,“MyTexttwo”中正在识别“MyText”。每个“名称”实际上都是唯一的,但它并没有像唯一一样被处理。我知道这可以在第一次出现时替换,但只能使用 RegEx。但我无法将 xml 转换为正则表达式并再次返回。还有什么我可以做的吗?
【问题讨论】:
-
请采纳这个普遍的建议。 从不、从不、从不对 XML 源代码使用字符串替换工具。这总是完全错误的做法。很难解释这是多么错误,因为对于初学者来说它看起来很容易。简单地决定永远不要这样做。学习正确的工具(在本例中:XPath)并使用它们。
-
@Tomalak Point,编辑了我的答案。也许您可以将您的警告作为 Blockquote 放在您的答案中,以便更好地脱颖而出?
-
@Tomalak 注意!
标签: xml powershell replace