Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如果每个记录的元素具有相似的标记,则获取正确的XML值_Php_Xml_Xpath_Xmlreader - Fatal编程技术网

PHP-如果每个记录的元素具有相似的标记,则获取正确的XML值

PHP-如果每个记录的元素具有相似的标记,则获取正确的XML值,php,xml,xpath,xmlreader,Php,Xml,Xpath,Xmlreader,PHP-如果每个记录的元素具有相似的标记,则获取正确的XML值 我正在获取以下xml文件: XML文件: <?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:

PHP-如果每个记录的元素具有相似的标记,则获取正确的XML值

我正在获取以下xml文件:

XML文件:

<?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>
然而。。。如果每个周期记录具有相同的标记定义,如何获取正确的“周期”值

期望输出:

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"

您可以简单地使用XPath的索引表示法;将
元素视为数组中的元素。请记住,XPath索引是基于一的,而不是基于零的

...
    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),
      ]
    );
...
输出:

5EXZX4LPK,"I Bornheim"
5967007LI,"SUN BANK"
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

您可以使用该类型对时段进行分组并获取第一个分组结果:

$xpath->evaluate(
    'string(
        abc:Entity/abc:Periods/abc:Period[
            abc:Type = "ACCOUNTING" or abc:Type="BALANCED"
        ][1]/abc:Type
    )', 
    $node
),
或者检查这里是否有
abc:End
子元素:

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