dou426098 2016-06-23 19:30
浏览 72
已采纳

xpath / php - 使用php选择包含特定属性的XML元素

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

  • 写回答

3条回答 默认 最新

  • dtg25862 2016-06-30 16:25
    关注

    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 />";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器