Извлечение данных из источника XML с использованием powershell - PullRequest
0 голосов
/ 10 октября 2019

У меня есть XML-файл с более чем 30 тыс. Записей. Идея заключается в поиске по номеру жизни и создании таблиц на основе информации, связанной с данными этого человека. По какой-то причине основной источник «Персона» не связан с подисточником «Назначение». Я не могу понять, как связать их. Мне удалось создать две функции для каждого набора. Но я вкратце пытаюсь связать оба в одну функцию. Где я могу запросить номер жизни и собрать вместе данные о персоне / встрече.

Образец исходных данных: (Создайте тестовый 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
}

1 Ответ

0 голосов
/ 10 октября 2019

Вы можете искать 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...