2017-02-14 50 views
1

我與父節點hospitalStore和幾個子節點的XML文件來訪問它的兄弟姐妹,我需要過程:查找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

爲了找到一個hospital有匹配條件的子節點category

  • 的PowerShell 4+:

    $hospital = $hospitals.Where({ $_.category -eq $foo }, 'first')[0] 
    
  • 的PowerShell 3+:

    $hospital = $hospitals | Where category -eq $foo 
    

    別名:Where = Where-Object = ?

  • 的PowerShell 1+:

    $hospital = $hospitals | Where { $_.category -eq $foo } 
    

    別名:Where = Where-Object = ?

  • 的XPath(適用於所有PS版本的Windows,但尚未PowerShell for Linux AFAIK):

    $hospital = $xml.SelectSingleNode("//hospital[category='$foo']") 
    

PowerShell 1,2,3方法返回一個包含所有匹配節點或單個匹配值(不是數組)的數組,或者返回一個迭代整個數組的數組。 PowerShell 4方法和SelectSingleNode在第一次匹配時停止。

所以樣板代碼可能是這樣的:

$xml = New-Object XML 
$xml.Load('C:\move\outpatient.xml') 

$hospitals = $xml.hospitalStore.hospital 

Get-ChildItem $src -File | ForEach { 
    $file = $_ 
    $fileCategory = $file.BaseName.Substring(0,6) 
    $fileName = $file.Name 

    $hospital = $hospitals | Where { $_.category -eq $fileCategory } 
    if (!$hospital) { 
     Write-Error "$fileCategory not found in XML" 
     return # return because we're inside a function-like ScriptBlock 
    } 
    # use the values 
    echo $hospital.criteria 
    echo $hospital.name 
    echo $hospital.destination 
    echo $hospital.hospcode 
    echo '' 
} 
相關問題