2016-10-20 7 views
0

入力XMLファイルがあり、を<AddressRow>のノード01-01-1995より前に削除するとします。日付条件が満たされていればXMLノードを削除

これは、基準を満たすサンプルXMLファイルです。

<?xml version="1.0" encoding="UTF-8"?> 
<OrganisationUnits> 
    <OrganisationUnitsRow num="1"> 
     <OrganisationId>TEST1</OrganisationId> 
     <OrganisationName>TEST PROVIDER</OrganisationName> 
     <Addresses> 
      <AddressesRow num="1"> 
      <AddressId>G72261</AddressId> 
      <MainAddress>N</MainAddress> 
      <NonStandardAddress> 
       <LocationId>P1031587</LocationId> 
       <StreetNumber>20</StreetNumber> 
       <Street>UNION ROAD</Street> 
       <Town>SOLIHULL</Town> 
       <PostCode>B90 3DQ</PostCode> 
      </NonStandardAddress> 
      <StartDate>1970-12-10</StartDate> 
      <EndDate>1994-12-11</EndDate> 
      <AddressType/> 
      </AddressesRow> 
     </Addresses> 
     <AcPayRef2>294288</AcPayRef2> 
     <Obsolete>N</Obsolete> 
    </OrganisationUnitsRow> 
</OrganisationUnits> 

ファイルを読み込んでノードokを削除する方法が分かっている場合は、問題なく動作します。

#Loop through the xml file and look for the node <EndDate> that exists under CMCOrganisationUnits.CMCOrganisationUnitsRow.Addresses.CMCAddressesRow 
foreach ($CMCAddressesRow in $xml.CMCOrganisationUnits.CMCOrganisationUnitsRow.Addresses.CMCAddressesRow) 
    { 
    #set a variable for the the node that we want to remove 
    $n = $CMCAddressesRow.Item('EndDate') 

    #if the node (EndDate) does exist 
    if ($n) { 

     #it exists then delete the parent node also 
     #$n.ParentNode.ParentNode.RemoveChild($n.ParentNode) 
     } 
    } 
    #save the changes to the file 
    $xml.Save("$path\$xml_out") 

これは、日付があるかどうかにかかわらず削除されます。私が把握できないのは、日付の値をテストする方法です。私はこのようにメインループからコールバックを成功させることなく使用しようとしました。

#Check the End date minimum value here 
$adr_end_callback = { 
    param($match) 
    $current = [DateTime]$match.Groups[1].Value 
    $minimum = [DateTime]'1995-01-01' 

     if ($minimum -gt $current) 
     { 
     '<EndDate>1995-01-01</EndDate>' 
     } 
     else { 
    '<EndDate>' + $match.Groups[1].Value + '</EndDate>' 
     } 
    } 

多分私はそれを考えすぎており、簡単な答えがあるかもしれません。

+0

[powershell - 日付を解析する方法は?](// stackoverflow.com/a/11393630)を参照してください。 – wOxxOm

答えて

1
$cutoffDate = Get-Date 1995-01-01 
$date = Get-Date $CMCAddressesRow.EndDate 

if ($date -lt $cutoffDate) { 
    # delete 
} 
関連する問題