PHP - Fetch correct XML values if elements have similar tags per record

不羁岁月 提交于 2019-12-11 04:13:46

问题


PHP - Fetch correct XML values if elements have similar tags per record

I'm fetching the below xml file:

XML file:

<?xml version='1.0' encoding='UTF-8'?>
<abc:ABCData xmlns:abc="http://www.abc-example.com" xmlns:xyz="http:/www.xyz-example.com">
<abc:ABCRecords>
 <abc:ABCRecord>
 <abc:ABC>5EXZX4LPK</abc:ABC>
  <abc:Entity>
    <abc:Name>I Bornheim</abc:Name>
    <abc:Periods>
      <abc:Period>
        <abc:Start>2017-01-01</abc:Start>
        <abc:End>2017-12-31</abc:End>
        <abc:Type>ACCOUNTING</abc:Type>
      </abc:Period>
      <abc:Period>
        <abc:Start>2007-09-01</abc:Start>
        <abc:Type>RELATIONSHIP</abc:Type>
      </abc:Period>
    </abc:Periods>      
  </abc:Entity>
</abc:ABCRecord>
<abc:ABCRecord>
  <abc:ABC>5967007LI</abc:ABC>
  <abc:Entity>
    <abc:Name>SUN BANK</abc:Name>
    <abc:Periods>
      <abc:Period>
        <abc:Start>2018-01-01</abc:Start>
        <abc:End>2018-12-31</abc:End>
        <abc:Type>BALANCED</abc:Type>
      </abc:Period>
      <abc:Period>
        <abc:Start>2008-09-01</abc:Start>
        <abc:Type>PARENT</abc:Type>
      </abc:Period>
    </abc:Periods>          
  </abc:Entity>
</abc:ABCRecord>
</abc:ABCRecords>
</abc:ABCData>

... with this script I output the values as csv:

PHP file:

<?php

$reader = new XMLReader();
$reader->open('php://stdin');

$output = fopen('php://stdout', 'w');
fputcsv($output, ['id', 'name']);

$xmlns = [
  'abc' => 'http://www.abc-example.com'
];

$dom   = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
  $xpath->registerNamespace($prefix, $namespaceURI);
}

while (
  $reader->read() && 
  (
    $reader->localName !== 'ABCRecord' || 
    $reader->namespaceURI !== $xmlns['abc']
  )
) {
  continue;
}

while ($reader->localName === 'ABCRecord') {
  if ($reader->namespaceURI === 'http://www.abc-example.com') {
    $node = $reader->expand($dom);
    fputcsv(
      $output, 
      [
        $xpath->evaluate('string(abc:ABC)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Name)', $node)
      ]
    );
  }

  $reader->next('ABCRecord');
} 

... like this:

Output:

5EXZX4LPK,"I Bornheim"
5967007LI,"SUN BANK"

Yet ... how do I fetch the correct 'period' values if each period record has the same tag definition?

Desired Output:

5EXZX4LPK,"I Bornheim",2017-01-01,2017-12-31,"ACCOUNTING",2007-09-01,"RELATIONSHIP"
5967007LI,"SUN BANK",2018-01-01,2018-12-31,"BALANCED",2008-09-01,"PARENT"

回答1:


You could use the type to group periods and fetch the first of the grouped results:

$xpath->evaluate(
    'string(
        abc:Entity/abc:Periods/abc:Period[
            abc:Type = "ACCOUNTING" or abc:Type="BALANCED"
        ][1]/abc:Type
    )', 
    $node
),

Or you check if here is an abc:End child element:

$xpath->evaluate(
    'string(
        abc:Entity/abc:Periods/abc:Period[
            count(abc:End) = 0
        ][1]/abc:Type
    )', 
    $node
),



回答2:


You can simply use XPath's index notation; think of the <abc:Period> elements as being in an array. Remember that XPath indices are one-based, not zero-based.

...
    fputcsv(
      $output, 
      [
        $xpath->evaluate('string(abc:ABC)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Name)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Periods/abc:Period[1]/abc:Start)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Periods/abc:Period[1]/abc:End)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Periods/abc:Period[1]/abc:Type)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Periods/abc:Period[2]/abc:Start)', $node),
        $xpath->evaluate('string(abc:Entity/abc:Periods/abc:Period[2]/abc:Type)', $node),
      ]
    );
...

Output:

id,name
5EXZX4LPK,"I Bornheim",2017-01-01,2017-12-31,ACCOUNTING,2007-09-01,RELATIONSHIP
5967007LI,"SUN BANK",2018-01-01,2018-12-31,BALANCED,2008-09-01,PARENT


来源:https://stackoverflow.com/questions/53797403/php-fetch-correct-xml-values-if-elements-have-similar-tags-per-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!