简体   繁体   中英

xpath/php - Select XML element that contain specific attributes using php

I'm trying to read data from nodes that have the same attribute names. I would like to read the lines with tag="650" into 3 separate variables. ie: each time I'm in a catalog node I need to read the given subjects.

<report>
<catalog>
    <flexibleKey>123</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 1</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 2</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 3</marcEntry>
    </marc>
</catalog>
<catalog>
    <flexibleKey>456</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject A</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject B</marcEntry>
    </marc>
</catalog>
</report>

My current code is below...

$z = new XMLReader;
$z->open('my.xml');

while ($z->read() && $z->name !== 'catalog');

while ($z->name === 'catalog') {
   $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
   echo $node->flexibleKey;
   echo $node->dateCreated;

   foreach ($node->marc->marcEntry as $tag) {
      // now I get lost :(
   }
}

Thanks for any help you can offer :)

You can use SimpleXMLElement class to represents an element in an XML document. Also you can use xpath() method to select custom attribute.

$xml = new SimpleXMLElement("xmlFile.xml");
$result = $xml->xpath("//*[@tag='650']");

foreach($result as $element)
{
    echo $element[0];
}

In above code xpath("//*[@tag='650']") select every element that contain tag='650' attribute.

You can test above code in demo

DOMDocument and DOMXPath to the rescue:

<?php
$doc = new DOMDocument();
$doc->load('my.xml');

$xpath = new DOMXPath($doc);
$results = $xpath->query("/catalog/marc/marcEntry[@tag = '650']");

foreach ($results as $result) {
    echo $result->nodeValue;    
}
?>

This did the trick...

$z = new XMLReader;
$z->open('my.xml');

while ($z->read() && $z->name !== 'catalog');

    while ($z->name === 'catalog') {
        $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
        echo $node->flexibleKey."<br />";
        echo $node->dateCreated."<br />";

        $result = $node->marc->xpath('marcEntry[@tag="650"]');
        foreach ($result as $subjecttag) {
            echo $subjecttag[0]."<br />";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM