powershell 脚本 - 使用 xmlPath 获取属性值,其中使用了多个属性过滤器

powershell script - obtain an attribute value using xmlPath where multiple attribute filters are used

我有多个 XML 文件,我必须从中提取数据并准备包含某些数据的 CSV 文件。

我需要找到一个元素包含多个属性的属性值,我也需要对这些属性进行过滤。

在下面的示例中,请问如何在 AuthID csv 字段中获取“123456AB”的值?我不仅尝试了以下 5 个示例,但都没有成功。

请注意,xml 和代码已被简化 (code/structure/values),因为我无法 post 整个示例。

谢谢

XML 摘录:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="somestylesheet.xsl" type="text/xsl"?>
<MyDocument xmlns:ext="namespace1" xmlns:xsi="namespace2" xmlns="defaultnamespace">
  <ext:asEntityIdentifier classCode="IDENT">
    <ext:id root="123.456.0.12341" assigningAuthorityName="ABC-1" />
  </ext:asEntityIdentifier>
  <ext:asEntityIdentifier classCode="IDENT">
    <ext:id root="1656.123.2" extension="123456AB" assigningAuthorityName="Some Auth Name" />
  </ext:asEntityIdentifier>
</MyDocument>

Powershell 脚本摘录

# Read in the xml file content
[xml]$xmlData = Get-Content -Path "C:\temp\mytest.xml"

# Declare the multiple XML Namespaces required
$nsMngr = New-Object System.Xml.XmlNameSpaceManager($xmlData.NameTable)
$nsMngr.AddNamespace("ns", $xmlData.DocumentElement.NamespaceURI)   # default
$nsMngr.AddNamespace("ext", "namespace1")

# Prepare a PSCustomObject of the relevant xml data
$reqdData = [PSCustomObject]@{

    AuthID1 $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]", $nsMngr)

    AuthID2 $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]", $nsMngr) | select extension

    AuthID3 $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]/extension", $nsMngr)
    
    AuthID4 $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]/ext:extension", $nsMngr)

    AuthID5 $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]/@extension", $nsMngr)

}

# Write the relevant data to a csv file
$reqdData | Export-Csv -Path $extractFile -NoTypeInformation -Force -Append

# CSV content: AuthID1 = "System.Xml.XmlElement", AuthID2 = "@{extension=123456AB}", AuthID3 and 4 both blank, AuthID5 = "System.Xml.XmlAttribute"
$reqdData = [PSCustomObject]@{

    AuthID = $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]", $nsMngr) | select -ExpandProperty extension

}