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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘