I'd like to get the content of the attribute xsi:schemaLocation. It's works perfectly with getElementsByTagName in php (and foreach after) but it's ugly, right ?
我想获取属性xsi:schemaLocation的内容。它与php中的getElementsByTagName(以及以后的foreach)非常匹配,但是它很难看,不是吗?
How to get the same content with a simple Xpath query ?
如何使用简单的Xpath查询获得相同的内容?
Here a short example of the xml content :
这里是xml内容的一个简短示例:
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="blabla" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
...
</gpx>
Thanks!
谢谢!
2 个解决方案
#1
1
Using the SimpleXMLElement class you can easily get the attribute xsi:schemaLocation's value:
使用SimpleXMLElement类,您可以轻松获得属性xsi:schemaLocation的值:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="blabla" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
</gpx>
XML;
$sxe = new SimpleXMLElement($xml);
$schemaLocation = $sxe->attributes('xsi', true)->schemaLocation;
echo (string) $schemaLocation;
#2
3
Typically you need to register the namespaces you want to use with the XPath library first. Then you can query the attribute by including namespace prefix along with the name.
通常,您需要首先在XPath库中注册要使用的名称空间。然后可以通过包含名称空间前缀和名称查询属性。
So let's assume you're using DOMXPath, you might register the following namespaces:
假设您使用的是DOMXPath,您可以注册以下名称空间:
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
$xpath->registerNamespace("gpx", "http://www.topografix.com/GPX/1/0");
And then you can query the schemaLocation attribute with something like this:
然后你可以用这样的方式查询schemaLocation属性:
$xpath->query("/gpx:gpx/@xsi:schemaLocation",$doc);
#1
1
Using the SimpleXMLElement class you can easily get the attribute xsi:schemaLocation's value:
使用SimpleXMLElement类,您可以轻松获得属性xsi:schemaLocation的值:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="blabla" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
</gpx>
XML;
$sxe = new SimpleXMLElement($xml);
$schemaLocation = $sxe->attributes('xsi', true)->schemaLocation;
echo (string) $schemaLocation;
#2
3
Typically you need to register the namespaces you want to use with the XPath library first. Then you can query the attribute by including namespace prefix along with the name.
通常,您需要首先在XPath库中注册要使用的名称空间。然后可以通过包含名称空间前缀和名称查询属性。
So let's assume you're using DOMXPath, you might register the following namespaces:
假设您使用的是DOMXPath,您可以注册以下名称空间:
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
$xpath->registerNamespace("gpx", "http://www.topografix.com/GPX/1/0");
And then you can query the schemaLocation attribute with something like this:
然后你可以用这样的方式查询schemaLocation属性:
$xpath->query("/gpx:gpx/@xsi:schemaLocation",$doc);