使用 PHP 过滤 XML 文件 [英] Filtering XML file, with PHP

查看:28
本文介绍了使用 PHP 过滤 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载 XML 文件,然后删除所有 ,其中 大于/大于 7 年.日期格式为 YYYY-MM-DD.

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

XML 示例:

<BackgroundReports userId="" password="" account="" >
    <BackgroundReportPackage>
        <Screenings>
            <Screening type="criminal" qualifier="">
                <CriminalReport>
                    <CriminalCase>
                        <AgencyReference type="Docket">
                            <IdValue>CR-0870120-09</IdValue>
                        </AgencyReference>
                        <Charge>
                            <ChargeId>
                                <IdValue>1</IdValue>
                            </ChargeId>
                            <ChargeOrComplaint>DUI: HIGHEST RTE OF ALC (BAC .16+) 1ST OFF</ChargeOrComplaint>
                            <ChargeTypeClassification>unknown</ChargeTypeClassification>
                            <DispositionDate>2009-04-07</DispositionDate>
                        </Charge>
                        <Charge>
                            <ChargeId>
                                <IdValue>2</IdValue>
                            </ChargeId>
                            <ChargeOrComplaint>CARELESS DRIVING</ChargeOrComplaint>
                            <ChargeTypeClassification>unknown</ChargeTypeClassification>
                            <DispositionDate>2010-08-02</DispositionDate>
                        </Charge>
                        <Charge>
                            <ChargeId>
                                <IdValue>3</IdValue>
                            </ChargeId>
                            <ChargeOrComplaint>STATUTE: 475 PC</ChargeOrComplaint>
                            <ChargeTypeClassification>misdemeanor</ChargeTypeClassification>
                            <OffenseDate>1988-11-05</OffenseDate>
                            <Disposition>CONVICTED</Disposition>
                            <DispositionDate>1988-11-09</DispositionDate>
                            <DispositionDate>1988-11-05</DispositionDate>
                            <DispositionDate>1988-11-09</DispositionDate>
                        </Charge>
                    </CriminalCase>
                </CriminalReport>
            </Screening>
        </Screenings>
    </BackgroundReportPackage>
</BackgroundReports>

我知道如何使用 PHP 打开和关闭/保存文件,我不知道如何删除我不想要的部分...如果有人愿意帮助我,我将非常感谢!

I know how to open and close/save file using PHP, I don't know how to delete the part i don't want... If anyone would help me with that I would be extremly thankfull!

推荐答案

您可以使用 SimpleXMLDOMXSL.

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

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

<小时>

使用 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();

<小时>

使用 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();

<小时>

使用 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>

然后使用这个PHP代码来转换它

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);

这篇关于使用 PHP 过滤 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆