簡體   English   中英

使用 powershell 從 XML 源中提取數據

[英]Extracting data from XML source using powershell

我有一個包含超過 30k 個條目的 xml 文件。 想法是按 lifenumber 搜索並根據與該人的數據相關聯的信息創建表格。 出於某種原因,主要來源“人員”沒有鏈接到“約會”下方的子來源。 我無法弄清楚如何鏈接這些。 我能夠創建兩個函數來拉每個集合。 但是我試圖將兩者鏈接到一個 function 中。 我可以在哪里查詢 lifenumber 並將人員/約會數據拉到一起。

源數據示例:(以下面的確切格式創建測試 xml)

  <Person lifenumber="21596" lname="LOVER" mname="J" fname="JERRY" affiliation="Hospital One" email="jerry.LOVER@mss.edu" building="" floor="" room="" phone="" hrstatus="A" active_direct_user="lassej04" active_direct_provider="HOSPITAL">
    <Appointment affiliation="Hospital One" deptname="Cardiology" divname="" deptcode="821" title="ASST CLIN PROF"/>
  </Person>
  <Person lifenumber="27901" lname="WINNER" mname="" fname="KURT" affiliation="Hospital One" email="kurt.WINNER@mss.edu" building="Annenberg" floor="17 TH FL" room="17-44" phone="(212) 241-1234" hrstatus="A" active_direct_user="hirsck01" active_direct_provider="MSSMCAMPUS">
    <Appointment affiliation="Hospital One" deptname="Pediatrics" divname="" deptcode="852" title="PROF LECTR"/>
  </Person>
  <Person lifenumber="30899" lname="OLYMPIA" mname="R" fname="MARTIN" affiliation="Hospital One" email="martin.OLYMPIA@mss.edu" building="" floor="" room="" phone="" hrstatus="A" active_direct_user="gellem03" active_direct_provider="HOSPITAL">
    <Appointment affiliation="Hospital One" deptname="Neurology" divname="" deptcode="841" title="ASSOC CLN PROF"/>
    <Appointment affiliation="Hospital One" deptname="Neurology" divname="" deptcode="105" title="ASSOC ATTN"/>
  </Person>
  <Person lifenumber="31183" lname="SCOOBY" mname="" fname="JAMES" affiliation="Hospital Two" email="" building="" floor="" room="" phone="" hrstatus="A" active_direct_user="" active_direct_provider="">
    <Appointment affiliation="Elmhurst/Queens Hospital" deptname="Otolaryngology" divname="" deptcode="A35" title="O.R. TECH"/>
  </Person>


-------------------------------------------------------------------------------------------------
Functions BUILT BELOW

$xmlPath = 'C:\Scripts\source.xml'
[xml]$data = Get-Content $xmlPath

#pulls everything above 'Appointment"
function Search-LifeNumber 
{
    param(
        $Source = $data,
        [Parameter(Mandatory=$true,
        Position=0)]
        $LifeNumber

    )

    $Target = $Source.People.Person | ? {$_.LifeNumber -eq $LifeNumber} 

    return $Target
}
#pulls Deptment Code data associated with targeted node
function Search-ByDepartmentCode 
{
    param(
        $Source = $data,
        [Parameter(Mandatory=$true,
            Position=0)]
        $DepartmentCode

    )

    $Target = $Source.SelectNodes('//People/Person/Appointment') | ? {$_.deptcode -eq $DepartmentCode} 

    return $Target
}

您可以根據屬性值搜索 XML。 使用 XPath 語法/path/to/node[@attribute="value"] 像這樣,

[xml]$p = get-content c:\temp\people.xml

# Search by Appointment at deptcode A35
$nl = $p.SelectNodes('/People/Person/Appointment[@deptcode="A35"]')
# How many results?
$nl.Count 
1
# Check the deptname
$nl[0].deptname
Otolaryngology
# Check the parent node's affliation?
$nl[0].parentnode.affiliation
Hospital Two


# Search by lifenumber
$nl = $p.SelectNodes('/People/Person[@lifenumber="30899"]')
# Check the email
$nl.email
martin.OLYMPIA@mss.edu
# How many appointments?
$nl.appointment.count
2
# See the titles
$nl.appointment | % { $_.title }
ASSOC CLN PROF
ASSOC ATTN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM