XML检索具有其属性的项



我有一个类似的XML:

<DescriptionsComplementaires>
    <DetailDescriptionComplementaire lang="fr" libelle="Descriptif" type="16.01.04">
        <Description lang="fr" libelle="Tarifs en clair" type="16.02.67">Gratuit</Description>
    </DetailDescriptionComplementaire>
    <DetailDescriptionComplementaire lang="fr" libelle="Descriptif" type="16.01.04">
        <Description lang="fr" libelle="Présentation, descriptif commercial" type="16.02.30">Chaussure de marche conseillées. De 12h à 14h sur présentation du ticket remis au départ: Apéritif offert par la commune des Haies Assiette dégustation de produits du terroir offerte pas la communauté de communes. Petit marché de producteurs locaux. Animations et stands d'information. Exposition "les 100 paniers du monde" Café et buvette. 
        </Description>
    </DetailDescriptionComplementaire>
</DescriptionsComplementaires>
    ........

如何在php中检索libelle="XXX"的项?我使用DOMDocument()。

编辑:

$dom = new DOMDocument;
$dom->load('(629)_ListeOI_fr_20120720_043502.xml');
$attr = "Présentation, descriptif court";
$query = "//*[@libelle='{$attr}']";
$xpath = new DOMXPath($dom);
$entries = $xpath->query($query);
foreach ($entries as $entry) {
    //i don't know what to do here for display items
}

DomDocument xpath

$dom = new DOMDocument;
$dom->loadXML($xml_string);
$attr = "Descriptif";
$query = "//*[@libelle='{$attr}']";
$xpath = new DOMXPath($dom);
$entries = $xpath->query($query);

要迭代结果:http://www.php.net/manual/en/domxpath.query.php

使用PHP的SimpleXML::XPathhttp://php.net/manual/en/simplexmlelement.xpath.php而不是你的方法。你的代码将被简化,看起来像这样:

$xml = new SimpleXMLElement($xmlStr); 
$res = $xml->xpath("//*[@libelle=XXX]"); 
print_r($res);