在XML中查找值并从同一父组访问其兄弟

时间:2017-02-14 04:31:31

标签: arrays xml powershell

我有一个带有父节点hospitalStore的XML文件和一些我需要处理的子节点:

<?xml version="1.0" encoding="UTF-8"?><!--Hospital Store -->
<hospitalStore><!-- Hospital Config -->
    <src><!--Search Path For Files To Move-->
        <path>C:\Users\Desktop\move\*.pdf</path>
    </src>
    <hospital>
        <criteria>Beth Israel requirements</criteria>
        <name>Beth Israel</name>
        <category>OPBEIH</category>
        <destination>C:\Users\Desktop\dest\1\</destination>
        <hospcode>1101</hospcode>
    </hospital><!-- End Hospital Config --><!-- Hospital Config -->
    <hospital>
        <criteria>Beth Israel CCC - WEST requirements</criteria>
        <name>Beth Israel CCC WEST</name>
        <category>OPBICC</category>
        <destination>C:\Users\Desktop\dest\2\</destination>
        <hospcode>1107</hospcode>
    </hospital><!-- End Hospital Config --><!-- Hospital Config -->
    <hospital>
        <criteria>Beth Israel KHD requirements</criteria>
        <name>Beth Israel KHD</name>
        <category>OPBIKD</category>
        <destination>C:\Users\Desktop\dest\3\</destination>
        <hospcode>1102</hospcode>
    </hospital><!-- End Hospital Config --><!-- Hospital Config -->
    <hospital>
        <criteria>Beth Israel KHD requirements</criteria>
        <name>Beth Israel KHD</name>
        <category>OPBIKE</category>
        <destination>C:\Users\Desktop\dest\3\</destination>
        <hospcode>1102</hospcode>
    </hospital><!-- End Hospital Config -->
</hospitalStore><!-- End Hospital Store -->

到目前为止,我的代码检索目录中的文件,然后将basename的前6个字符与XML子节点category进行匹配。

我希望能够将与XML category中的值匹配的文件指向XML destination等,然后使用其他一些函数测试目标。我不确定如何将文件与该组节点相关联。

$cPath="C:\move\outpatient.xml"
$xml = New-Object -TypeName XML
$xml.Load($cPath)
$hName = $xml.hospitalstore.hospital
$src = $xml.hospitalstore.src.path
$fileList= get-childitem -path $src -File

ForEach ($file in $fileList ){

$category = ($file.BaseName.Substring(0,6))
$fileName = $fileList.Name


if ($category -in $hName.category){


$file.Name
$hName.category

}

}

1 个答案:

答案 0 :(得分:1)

要查找具有与条件匹配的子节点SHA-1 certificate fingerprint的{​​{1}}:

  • PowerShell 4 +:

    hospital
  • PowerShell 3 +:

    category

    别名:$hospital = $hospitals.Where({ $_.category -eq $foo }, 'first')[0] = $hospital = $hospitals | Where category -eq $foo = Where

  • PowerShell 1 +:

    Where-Object

    别名:? = $hospital = $hospitals | Where { $_.category -eq $foo } = Where

  • XPath(适用于Windows的所有PS版本,但尚未在PowerShell for Linux AFAIK中使用):

    Where-Object

PowerShell 1,2,3方法返回所有匹配节点的数组,或单个匹配值(不是数组),或者什么都不返回迭代整个数组。 PowerShell 4方法和SelectSingleNode在第一场比赛时停止。

所以样板代码可能是这样的:

?