我有一个超过30k条目的xml文件。这个想法是根据生命数字搜索并根据与该人的数据相关联的信息创建表。由于某些原因,主要来源“人员”未链接到“约会”下方的子来源。我不知道如何链接这些。我能够创建两个函数来提取每个集合。但是我很快就试图将两者都链接到一个函数中。在这里,我可以查询生活号码并将人/约会数据一起收集。
源数据示例:(以下面的确切格式创建测试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
}
答案 0 :(得分:0)
您可以基于属性值搜索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