【问题标题】:How to compare XML files using PowerShell while ignoring certain tags?如何在忽略某些标签的情况下使用 PowerShell 比较 XML 文件?
【发布时间】:2020-06-19 13:31:19
【问题描述】:

我正在使用 PowerShell 来比较两个不同的 xml 文件和以下代码:

Compare-Object (Get-Content C:\Users\Desktop\newtask3.xml | Where-Object { $_.Trim() -ne '' })(Get-Content C:\Users\Desktop\newtask4.xml | Where-Object { $_.Trim() -ne '' })

第一个 XML 文件:

<html>a
    <ID>GT-ANI-2016-05-02T21:01Z</ID>
    <CreationDate>2016-05-02T21:01:40</CreationDate>
    <Total> 5000 </Total>
    <type>ANI</type>
</html>

第二个 XML 文件:

<html>a
    <ID>GT-ANI-2016-05-03T21:06Z</ID>
    <CreationDate>2016-05-03T21:06:40</CreationDate>
    <Total> 5000 </Total>
    <type>ANI</type>
</html>

我的问题是:在比较上面的两个 xml 文件时,如何确保 PowerShell 脚本忽略 &lt;ID&gt;&lt;CreationDate&gt; 中的唯一字段?

【问题讨论】:

  • 您的问题似乎缺少部分内容。 “? 中的唯一字段”、“? 的重复值”。可能会被自动删除。您是否要求这个特定的 xml 或一般的 xml?你追求的结果是什么?根据 BenH 的回复,您可以导入为 [xml],这使您可以访问属性(和值)。乐于助人,只是不确定目标是什么。
  • 嗨,我已经稍微修改了我的问题,希望它更清楚我的意思。我还删除了重复值的部分,而只是想知道如何确保比较忽略某些标签。这些标签在我比较的两个 xml 文件之间是相同的,但如果我比较另一个 xml 文件,这些标签可能会有所不同

标签: xml powershell


【解决方案1】:

我希望这会有所帮助。我试图明确分解如何处理xml/html、PowerShell 中的属性和内置/手动比较。

# Import files, casting as xml type.
[xml]$html1 = Get-Content -Path .\1.xml
[xml]$html2 = Get-Content -Path .\2.xml

# Get the members of the html 1's properties where membertype is properties (i.e. the tags)
# See bottom comment for getting tags from both documents
$htmlTags  = ($html1.html | Get-Member | Where-Object {$_.membertype -eq "Property"}).Name

# Put the tags you would like to ignore in an array
$ignoreTheseTags = @("type","ID")

#Loop over the tags, ignoring the ones that you have put in the array
foreach ($tag in $htmlTags | Where-Object {$_ -notin $ignoreTheseTags}){

    # Get the values in this tag.
    $value1 = $html1.html.$tag
    $value2 = $html2.html.$tag

    # You can test to see if the tag is in file 2
    # Continue skips to next tag as Compare-Object will return an error if one of the objects is null
    if($value2 -eq $null){
        Write-Host "Tag $tag is not in html 2`n"
        continue
    }

    # PowerShell built-in comparison operator
    Compare-Object  $value1 $value2

    # Manual case-sensitive value comparison. You can do whatever you want inside the loop
    if($value1 -cne $value2){
        Write-Host "Tag $tag is not equal."
        Write-Host "First file value:  $value1"
        Write-Host "Second file value: $value2`n"
    }

}

<#
#Getting tags from both html documents
$htmlTags  = ($html1.html | Get-Member | Where-Object {$_.membertype -eq "Property"})
$htmlTags += ($html2.html | Get-Member | Where-Object {$_.membertype -eq "Property"})

$htmlTags = ($htmlTags | Select-Object Name -Unique).Name
#>

【讨论】:

    【解决方案2】:

    据我所知,没有简单的方法可以做到这一点。您可以将 xml 文档作为变量导入,然后找到共享属性并过滤掉那些是数组的属性。然后使用过滤后的属性列表进行比较。

    [xml]$XmlDocument1 = Get-Content -Path C:\example1.xml
    [xml]$XmlDocument2 = Get-Content -Path C:\example2.xml
    $xml1props = ($XmlDocument1.html | gm | ? {$_.membertype -eq "Property"  -AND ($_.definition.split(" "))[0] -eq "string"}).name
    $xml2props = ($XmlDocument2.html | gm | ? {$_.membertype -eq "Property"  -AND ($_.definition.split(" "))[0] -eq "string"}).name
    $sharedprops = (Compare-Object $xml1props $xml2props -IncludeEqual | ? {$_.sideindicator -eq "=="}).inputobject
    compare-object $XmlDocument1.html $XmlDocument2.html -Property $sharedprops
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 2021-03-03
      • 2022-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多