【问题标题】:powershell script - obtain an attribute value using xmlPath where multiple attribute filters are usedpowershell 脚本 - 使用 xmlPath 获取属性值,其中使用了多个属性过滤器
【发布时间】:2021-11-12 00:02:00
【问题描述】:

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

我需要找到一个元素包含多个属性的属性值,并且我还需要过滤这些属性。

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

请注意,xml 和代码已被简化(代码/结构/值),因为我无法发布整个示例。

谢谢

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"

【问题讨论】:

    标签: xml powershell attributes export-csv


    【解决方案1】:
    $reqdData = [PSCustomObject]@{
    
        AuthID = $xmlData.SelectSingleNode("//ns:MyDocument/ext:asEntityIdentifier[@classCode=""IDENT""]/ext:id[@assigningAuthorityName=""Some Auth Name""]", $nsMngr) | select -ExpandProperty extension
    
    }
    

    【讨论】:

    • 非常感谢。我在您发表评论后不久就发表了评论,但由于某种原因,评论不再存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2015-09-14
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多