获取根节点的 XML 个命名空间
Get XML namespace of root node
我收到 xml 赞:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://adress.pl/FeResourceServlet/localTemplate/template1/styl.xsl"?>
<wnio:Dokument
xmlns:adr="http://adress.pl/xml/schema/adress/2009/11/09/"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:inst="http://adress.pl/xml/schematy/instytucja/2009/11/16/"
xmlns:meta="http://adress.pl/xml/schematy/meta/2009/11/16/"
xmlns:oso="http://adress.pl/xml/schematy/osoba/2009/11/16/"
xmlns:str="http://adress.pl/xml/schematy/struktura/2009/11/16/" xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xsi:schemaLocation="http://adress.pl/FeResourceServlet/localTemplate/template1/ http://epuap.gov.pl/FeResourceServlet/localTemplate/template1/schema.xsd">
...
我的问题是 - 如何获得 root 的命名空间?
在我的根节点上方是 wnio:Dokument 并且我知道 wnio 是 "namespace of root" 并且根的名称是 Dokument.
但名称和命名空间可以更改。然后我将有根节点,但我不知道命名空间和根名称。
到目前为止我使用了:SimpleXMLElement::getNamespaces 和 SimpleXMLElement::getDocNamespaces。但是我收到了每个命名空间,但我不知道哪个是根。
PHP有可能得到这些信息吗?
您可以使用DomDocument
$dom = new DOMDocument();
$response = $dom->loadXML($xml);//$xml is your xml string or file
$root = $dom->documentElement;//will return the document root element
$rootPrefix = $root->prefix;//getting the prefix of your element
$namespace = $root->lookupNamespaceURI($rootPrefix);//getting the namespace of the root element
[documentElement][2]
属性是将根元素作为 DOMElement 获取的简单方法。
DOM节点有一个属性$namespaceURI
即returns节点的命名空间:
$document = new DOMDocument();
$document->loadXml(
'<wnio:Dokument xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"/>'
);
var_dump($document->documentElement->namespaceURI);
但是名称空间是 XML 文档中最稳定的部分。名称空间指定信息的格式。如果名称空间更改,格式也会更改,您将不得不更改应用程序的逻辑。
您的应用程序需要知道它读取的格式并期望得到它。那就是命名空间。
另一方面,名称空间前缀可以在任何元素节点上更改。您应该期待一个特定的前缀,但您可以并且必须期待一个特定的命名空间。
这是一个例子:
$document = new DOMDocument();
$document->loadXml(
'<wnio:Dokument xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"/>'
);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('w', 'http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/');
foreach ($xpath->evaluate('/w:Dokument') as $node) {
var_dump($node->nodeName);
}
输出:
string(13) "wnio:Dokument"
我收到 xml 赞:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://adress.pl/FeResourceServlet/localTemplate/template1/styl.xsl"?>
<wnio:Dokument
xmlns:adr="http://adress.pl/xml/schema/adress/2009/11/09/"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:inst="http://adress.pl/xml/schematy/instytucja/2009/11/16/"
xmlns:meta="http://adress.pl/xml/schematy/meta/2009/11/16/"
xmlns:oso="http://adress.pl/xml/schematy/osoba/2009/11/16/"
xmlns:str="http://adress.pl/xml/schematy/struktura/2009/11/16/" xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xsi:schemaLocation="http://adress.pl/FeResourceServlet/localTemplate/template1/ http://epuap.gov.pl/FeResourceServlet/localTemplate/template1/schema.xsd">
...
我的问题是 - 如何获得 root 的命名空间? 在我的根节点上方是 wnio:Dokument 并且我知道 wnio 是 "namespace of root" 并且根的名称是 Dokument.
但名称和命名空间可以更改。然后我将有根节点,但我不知道命名空间和根名称。
到目前为止我使用了:SimpleXMLElement::getNamespaces 和 SimpleXMLElement::getDocNamespaces。但是我收到了每个命名空间,但我不知道哪个是根。
PHP有可能得到这些信息吗?
您可以使用DomDocument
$dom = new DOMDocument();
$response = $dom->loadXML($xml);//$xml is your xml string or file
$root = $dom->documentElement;//will return the document root element
$rootPrefix = $root->prefix;//getting the prefix of your element
$namespace = $root->lookupNamespaceURI($rootPrefix);//getting the namespace of the root element
[documentElement][2]
属性是将根元素作为 DOMElement 获取的简单方法。
DOM节点有一个属性$namespaceURI
即returns节点的命名空间:
$document = new DOMDocument();
$document->loadXml(
'<wnio:Dokument xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"/>'
);
var_dump($document->documentElement->namespaceURI);
但是名称空间是 XML 文档中最稳定的部分。名称空间指定信息的格式。如果名称空间更改,格式也会更改,您将不得不更改应用程序的逻辑。
您的应用程序需要知道它读取的格式并期望得到它。那就是命名空间。
另一方面,名称空间前缀可以在任何元素节点上更改。您应该期待一个特定的前缀,但您可以并且必须期待一个特定的命名空间。
这是一个例子:
$document = new DOMDocument();
$document->loadXml(
'<wnio:Dokument xmlns:wnio="http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/"/>'
);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('w', 'http://epuap.gov.pl/FeResourceServlet/localTemplate/ZgloszenieBudowy/');
foreach ($xpath->evaluate('/w:Dokument') as $node) {
var_dump($node->nodeName);
}
输出:
string(13) "wnio:Dokument"