simplexml_load_file未在gpx上加载xml扩展名称空间

时间:2018-07-08 10:06:34

标签: php simplexml xml-namespaces

我正在尝试从GPX文件中加载一些数据。

该文件已从Garmin Connect(https://connect.garmin.com)下载,并且具有一些自定义扩展名,例如心律等。出于某种原因,simplexml_load_file()跳过了这些扩展名,无法为其注册名称空间他们。

要清楚,缺少两件事。最重要的是,输出中缺少ns3:*元素。

<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="Garmin Connect" version="1.1"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd"
  xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
  xmlns="http://www.topografix.com/GPX/1/1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
  >
   <metadata>
    <link href="connect.garmin.com">
      <text>Garmin Connect</text>
    </link>
    <time>2018-07-06T14:53:04.000Z</time>
  </metadata>
  <trk>
    <name>Stripped</name>
    <type>cycling</type>
    <trkseg>
      <trkpt lat="35.4119682700932025909423828125" lon="-2.3132956029832363128662109375">
        <ele>51.21000152587890625</ele>
        <time>2018-07-06T14:53:04.000Z</time>
        <extensions>
          <ns3:TrackPointExtension>
            <ns3:atemp>28.0</ns3:atemp>
            <ns3:hr>113</ns3:hr>
          </ns3:TrackPointExtension>
        </extensions>
     </trkpt>
     </trkseg>
     </trk>
</gpx>

真正简单的代码:

$foo = simplexml_load_file("test.xml");
print_r($foo->getNamespaces());
print_r($foo);

产生这个:

Array
(
    [] => http://www.topografix.com/GPX/1/1
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
#### Missing namespaces here ####

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [creator] => Garmin Connect
            [version] => 1.1
        )

    [metadata] => SimpleXMLElement Object
        (
            [link] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [href] => connect.garmin.com
                        )

                    [text] => Garmin Connect
                )

            [time] => 2018-07-06T14:53:04.000Z
        )

    [trk] => SimpleXMLElement Object
        (
            [name] => Stripped
            [type] => cycling
            [trkseg] => SimpleXMLElement Object
                (
                    [trkpt] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [lat] => 35.4119682700932025909423828125
                                    [lon] => -2.3132956029832363128662109375
                                )

                            [ele] => 56.40000152587890625
                            [time] => 2018-07-06T14:53:04.000Z
                            [extensions] => SimpleXMLElement Object
                                (
##### Missing extensions here #####
                                )

                        )

                )

        )

1 个答案:

答案 0 :(得分:0)

如: http://php.net/manual/en/simplexmlelement.getnamespaces.php

  

递归   如果指定,则返回在父节点和子节点中使用的所有名称空间。否则,仅返回在根节点中使用的名称空间(如果有的话),如果没有,则返回任何名称空间。

在子节点中使用名称空间ns3,然后需要将递归指定为true。 否则,您只能在XML根目录中获得ns:

     $foo = simplexml_load_file("test.xml");
     print_r($foo->getNamespaces(TRUE));