Filtering XML file, with PHP

前端 未结 2 484
一整个雨季
一整个雨季 2021-01-16 01:53

I want to load XML file and then remove all where is bigger/older then 7 years. Date format is YYYY-MM-DD.

相关标签:
2条回答
  • 2021-01-16 02:05

    You can either use SimpleXML, DOM or XSL for it.

    Example XML (shortened for brevity (from Revision 1 of your question)):

    $xml = <<< XML
    <CriminalCase>
        <Charge>
            <DispositionDate>1995-12-21</DispositionDate>
        </Charge>
        <Charge>
            <DispositionDate>2010-12-21</DispositionDate>
        </Charge>
    </CriminalCase>
    XML;
    

    With SimpleXml

    $sevenYearsAgo = new DateTime('-7 years');
    $CriminalCase  = new SimpleXmlElement($xml);
    for ($i = 0; $i < $CriminalCase->Charge->count(); $i++) {
        $dispositionDate = new DateTime($CriminalCase->Charge->DispositionDate);
        if ($dispositionDate < $sevenYearsAgo) {
            unset($CriminalCase->Charge[$i]);
        }
    }
    echo $CriminalCase->asXml();
    

    With DOM

    $dom = new DOMDocument;
    $dom->loadXml($xml);
    $xpath = new DOMXPath($dom);
    $oldCases = $xpath->query(
        sprintf(
            '//Charge[substring-before(DispositionDate, "-") < %d]',
            date('Y', strtotime('-7 years'))
        )
    );
    foreach ($oldCases as $oldCase) {
        $oldCase->parentNode->removeChild($oldCase);
    }
    echo $dom->saveXml();
    

    With XSLT

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:date="http://exslt.org/dates-and-times"
                    extension-element-prefixes="date">
    
      <xsl:output indent="yes" method="xml"/>
      <xsl:template match="/">
        <CriminalCase>
          <xsl:apply-templates />
        </CriminalCase>      
      </xsl:template>
      <xsl:template match="Charge">
        <xsl:if test="date:year(DispositionDate) &gt; date:year() - 7">
          <xsl:copy-of select="."/>
        </xsl:if>
      </xsl:template>   
    </xsl:stylesheet>
    

    and then use this PHP Code to transform it

    $doc = new DOMDocument();
    $xsl = new XSLTProcessor();
    $doc->loadXml($xsl);
    $xsl->importStyleSheet($doc);
    $doc->loadXml($xml);
    echo $xsl->transformToXML($doc);
    
    0 讨论(0)
  • 2021-01-16 02:09

    Here are some tips on how to get started:

    1. You need to parse the XML to something a little easier to work with. PHP has a library called SimpleXML.
    2. Loop through the data and remove the objects which are older than 7 years. To compare dates you have to first convert the dates you got from the XML to something PHP can process as a date. Take a look at strtotime which gives you the timestamp (seconds since 1970, actually 1901 for version > 5.1.0) or DateTime which supports dates before 1970.
    3. To check if the fetched date is older than 7 years ago, you need to (one way) subtract the timestamp with the current timestamp, and see if that value is greater than 7 years in seconds. Or if you use DateTime, you can take a look at DateTime::diff. Remove the objects that you iterate over that are older than 7 years (unset).
    4. To save as XML again, take a look at SimpleXMLElement::asXML

    Hope that helps!

    0 讨论(0)
提交回复
热议问题