首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell XML -根据另一个节点值从节点获取值

PowerShell XML -根据另一个节点值从节点获取值
EN

Stack Overflow用户
提问于 2022-08-22 22:12:29
回答 1查看 36关注 0票数 0

我对此有点陌生,所以任何帮助都是非常感谢的。

我需要返回<Amount>值,其中兄弟节点<Type>包含值"BurialFund“。

我使用的脚本(在XML示例下面)只返回来自<Asset>节点的第一个<Asset>值,因此我需要一种方法来根据<Type>值指定要从哪个节点提取。

这是我需要帮助的行:$burial = Select-Xml -XPath '//AssetParent/Assets/Asset/Amount'

除了获得我需要的值之外,这个脚本可以完成我想做的事情--从文件夹中读取、检索必要的值,并写入.csv文件。

提前谢谢你!

摘自我的XML:

代码语言:javascript
复制
<AssetParent>
    <Assets>
        <Asset>
            <Type>CheckingAccounts</Type>
            <Amount>100</Amount>
        </Asset>
        <Asset>
            <Type>SavingsAccounts</Type>
            <Amount>200</Amount>
        </Asset>
        <Asset>
            <Type>BurialFund</Type>
            <Amount>5000</Amount>
        </Asset>
    </Assets>
</AssetParent>

我的剧本:

代码语言:javascript
复制
# Set the directories where this script should read from and write the results to.
$dir = 'C:\Users\username\Documents\XML\BurialAssetExclusion\'
$manifest = 'C:\Users\username\Documents\XML\BurialAssetExclusion\' + (Get-Date -Format yyyyMMdd) + '.csv'

# Process the XML files.
Get-ChildItem -Path $dir -Filter *xml | ForEach-Object {

  # Retrieve values from specified nodes.
  $interviewDate = Select-Xml -XPath '//CurrentDate' -Path $_.FullName -ErrorAction SilentlyContinue
  $burial = Select-Xml -XPath '//AssetParent/Assets/Asset/Amount' -Path $_.FullName -ErrorAction SilentlyContinue
  $dob = Select-Xml -XPath '//DOB' -Path $_.FullName -ErrorAction SilentlyContinue
  $lastName = Select-Xml -XPath '//LastName' -Path $_.FullName -ErrorAction SilentlyContinue
  $firstName = Select-Xml -XPath '//FirstName' -Path $_.FullName -ErrorAction SilentlyContinue

  # If values were retrieved succesfully.
  if ($interviewDate -and $burial -and $dob -and $lastName -and $firstName) {

    # Create a custom PSObject and set the values to corresponding properties.
    # Out-String Trim used to eliminate System.Object[] error.
    New-Object PSObject -Property @{
      InterviewDate = ($interviewDate.Node.InnerText | Out-String).Trim()
      Burial = ($burial.Node.InnerText | Out-String).Trim()
      DOB = ($dob.Node.InnerText | Out-String).Trim()
      LastName = ($lastName.Node.InnerText | Out-String).Trim()
      FirstName = ($firstName.Node.InnerText | Out-String).Trim()
    }
  }
  
  # Clear values.
  Clear-Variable interviewDate, burial, dob, lastName, firstName
  
    # Set order of columns.
    # Export data to the CSV file.
  } | Select-Object InterviewDate, Burial, DOB, LastName, FirstName | Export-Csv -Path $manifest -NoTypeInformation -append
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-22 22:23:03

您可以获得Amount元素的文本节点,其中兄弟元素Type包含如下文本"BurialFund",使用XPath:

代码语言:javascript
复制
$burial = $xml | Select-Xml -XPath '//AssetParent/Assets/Asset[Type="BurialFund"]/Amount/text()'
$burial.Node.Value  # Outputs 5000

或者,当您将XML加载到XML文档中时,使用PowerShell提供的方便对象访问:

代码语言:javascript
复制
$xml = [xml]::new(); $xml.Load( $fullPathToXml )
$xml.AssetParent.Assets.Asset.Where{ $_.Type -eq 'BurialFund' }.Amount
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73451482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档