0

Wondering if someone can help me please. I've an xml file which has logs with date in this format - 2020-12-06T01:00:02.578+11:00.

I'd like to use xpath to search through the data and filter records between two dates. I'm not able to figure how I can do this via the xpath filter search itself.

Below is my code so far, the search filter does not work obvious reason as it is a text value. I need to covert this date first?

Below code of matching date using greater than symbol does not work.

**Sample XML:**
<data>
<faultRecord ack="no"
     cause="config-error"                
     Action="deletePresent"
     code="3564"
     created="2020-12-06T01:00:02.578+11:00"/>
</data>


$xml = simplexml_load_file('faults.xml');

$faults = new SimpleXMLElement('faults.xml',null,true);
  
foreach($faults->xpath('//data/faultRecord[@created > "2020-12-06"]') as $faultRecord) {        

    foreach($faultRecord->attributes() as $Attribute => $value)     
            
            if ($Attribute == 'created' or  $Attribute == 'description') {
                echo "{$Attribute} = {$value} <br /> <br /> ";                  
            }           
}         

Thank you

EDIT worked out a way to do this in case if it ever helps anyone:

    $faults = new SimpleXMLElement('faults.xml',null,true);
  
foreach($faults->xpath('//data/faultRecord[@code = "3564"]') as $faultRecord) {     
    
    $date = strtotime($faultRecord->attributes()->created); 
    
    if ($date > 1607176802) {
    foreach($faultRecord->attributes() as $Attribute => $value)                     
                        
        if ($Attribute == 'created' or  $Attribute == 'description') {
            echo "{$Attribute} = {$value} <br /> <br /> ";                  
        }           
        
    }
}
loneknight
  • 63
  • 8
  • Provide proper [mre]s of issue like this, please, include some XML sample data so that we can see what is located where. – CBroe Dec 09 '20 at 07:36
  • _“the search filter does not work obvious reason as it is a text value. I need to covert this date first?”_ - shouldn’t have to, you got a “sortable” date format here, so string comparison including greater/lesser should work fine. – CBroe Dec 09 '20 at 07:38
  • With DOM this can be done with Xpath calling back to PHP. https://stackoverflow.com/questions/23574055/filter-array-or-xml-with-time-between-start-and-end-time/23683347#23683347 – ThW Dec 09 '20 at 10:44
  • I thought I covered the question with enough information to describe the problem. Should at least give me a chance to respond rather than closing the question. Anyhow, I've now added the sample xml structure being used. Matching the date using text as per my code above does not work. – loneknight Dec 09 '20 at 23:30

1 Answers1

0

perhaps this could help you. XPath query filtering by date

Perhaps, For more help, you need the Build XML Structure.

B.Irneos
  • 31
  • 6