1
我的問題是,我有一個具有相同值的兩倍像一個XML文件:刪除從XML文件中的重複標籤
<ns:html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="2" release="1">
<ns:Name>A
<ns:ID>ANI-2016-05-02T21:01Z</ns:ID>
<ns:CreationDate>2016-05-02T21:01:40</ns:CreationDate>
<ns:Subname>A2
<ns:Total>5000</ns:Total>
<ns:type>ANI</ns:type>
</ns:Subname>
</ns:Name>
<ns:Name>A
<ns:ID>ANI-2016-05-02T21:01Z</ns:ID>
<ns:CreationDate>2016-05-02T21:01:40</ns:CreationDate>
<ns:Subname>A2
<ns:Total>5000</ns:Total>
<ns:type>ANI</ns:type>
</ns:Subname>
</ns:Name>
<ns:Name>A
<ns:ID>ANI-2016-08-04T21:01Z</ns:ID>
<ns:CreationDate>2016-04-08T21:01:40</ns:CreationDate>
<ns:Subname>A2
<ns:Total>5000</ns:Total>
<ns:type>ANI</ns:type>
</ns:Subname>
</ns:Name>
</ns:html>
我的問題是如何使用XML來從XML文件中刪除重複值以便在新的XML文件中獲得以下結果。支票可以在ID的基礎上完成。
<ns:html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="2" release="1">
<ns:Name>A
<ns:ID>ANI-2016-05-02T21:01Z</ns:ID>
<ns:CreationDate>2016-05-02T21:01:40</ns:CreationDate>
<ns:Subname>A2
<ns:Total>5000</ns:Total>
<ns:type>ANI</ns:type>
</ns:Subname>
</ns:Name>
<ns:Name>A
<ns:ID>ANI-2016-05-02T21:01Z</ns:ID>
<ns:CreationDate>2016-05-02T21:01:40</ns:CreationDate>
<ns:Subname>A2
<ns:Total>5000</ns:Total>
<ns:type>ANI</ns:type>
</ns:Subname>
</ns:Name>
</ns:html>
我曾嘗試以下的事情:
首先,我使用的例子作爲發現here:
## SETUP ENVIRONMENT
# Find "Advanced Monitoring Agent" service and use path to locate files
$gfimaxagent = Get-WmiObject Win32_Service |
Where-Object { $_.Name -eq 'Advanced Monitoring Agent' }
$gfimaxexe = $gfimaxagent.PathName
$gfimaxpath = Split-Path $gfimaxagent.PathName.Replace([char]34,"") -Parent #"Wordpress syntax highlighter bug
$XmlFile = "C:\Users\Desktop\Test.xml"
$Output = "C:\Users\Desktop\result.xml"
[xml]$XmlContent = Get-Content $XmlFile
$XmlPath = "checks"
$Property = "uid"
$XmlValues = @{}
foreach ($XmlElement in $XmlContent.$XmlPath.ChildNodes)
{
$ElementValues = "" #"Wordpress syntax highlighter bug
foreach($XmlValue in $XmlElement.ChildNodes | Sort-Object name)
{
$ElementValues = $ElementValues + $XmlValue.Name + $XmlValue.InnerText
}
$XmlValues[$XmlElement.$Property] = $ElementValues
}
$XmlDuplicates = @{}
foreach ($XmlValue in $XmlValues.Values)
{
$Items = @($XmlValues.Keys | Where { $XmlValues[$_] -eq $XmlValue })
if ($Items.Count -gt 1)
{
if (!($XmlDuplicates[$Items[0]])) { $XmlDuplicates[$Items[0]] = $Items }
}
}
foreach ($XmlDuplicate in $XmlDuplicates.Keys)
{
for ($i = 1; $i -lt $XmlDuplicates[$XmlDuplicate].Count; $i++)
{
$XPath = "//" + $XmlPath + "/*[@" + $Property +"=" + $XmlDuplicates[$XmlDuplicate][$i]+"]"
$ChildToBeRemoved = $XmlContent.SelectSingleNode($XPath)
$ChildToBeRemoved.ParentNode.RemoveChild($ChildToBeRemoved)
}
}
$XmlContent.Save($Output)
問題是即使我改變了源仍然沒有載入我的文件但來自網站的原始示例文件。
至於第二我也用下面的代碼試了一下:
[xml]$XmlDocument1 = Get-Content -Path C:\Users\EX27740\Desktop\testdubbel.xml
$softwareVersionsArray = $catalogXML.catalog.software |
Group-Object name |
ForEach-Object {$_.Group[0]}
$filename = ' C:\Users\EX27740\Desktop\Resultaat.xml'
$catalogXML.Save($filename)
但得到一個錯誤:通過XML節點
At line:8 Char:1 Cannot call a null-valued expression
你能提供一個根節點的例子嗎? –
SO不是其他人爲你工作的地方。你到目前爲止試過了什麼(顯示你的代碼),以及你需要什麼*特定的問題(在你的代碼中)? –
嗨對不起,添加thodse鈦現在加入他們 – Jarno343