Home > Software engineering >  powershell script - obtain an attribute value using xmlPath where multiple attribute filters are use
powershell script - obtain an attribute value using xmlPath where multiple attribute filters are use

Time:09-21

I have multiple XML files I have to extract data from and prepare a CSV file of certain data.

I need to find an attribute value where an element contains multiple attributes and I need to filter on the attributes too.

In the below example, how do I get the value of "123456AB" in an AuthID csv field please? I've tried more than just the 5 examples below with no success.

Note, the xml & code have been simplified (code/structure/values) as I can't post the whole example.

Thanks

XML excerpt:

<?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 script excerpt

# 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"

CodePudding user response:

$reqdData = [PSCustomObject]@{

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

}
  • Related