Using XML prefixes/namespaces to create an Xpath

你离开我真会死。 提交于 2019-12-23 23:12:40

问题


This is a follow on from my prior question. I think I jumped in at the deep end so spending some time fully understanding XML namespaces.

From this XML page, gonna focus on the below element for a min:

<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

I understand that the m: prefix has been used in order to distinguish the properties element from elements with the same name in other documents, in case such documents are combined. I understand that in order to use a prefix, a namespace for that prefix must be defined. Such a definition is done using the XML namespace attribute (xmlns) and the syntax is xmlns:prefix="URI". URI is the Uniform Resource Identifier, which is basically the element's source. URIs used to be called Uniform Resource Names (URN), which are essentially the same thing.

Now taking a look at the answer to my prior Q:

$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$element = simplexml_load_file($url);

$element->registerXPathNamespace(
  'atom', 'http://www.w3.org/2005/Atom'
);
$element->registerXpathNamespace(
  'meta', 'http://schemas.microsoft.com/ado/2007/08/dataservice/metadata'
);

foreach ($element->xpath('//atom:entry/atom:content/meta:properties') as $properties) {
  $properties->registerXpathNamespace('data', 'http://schemas.microsoft.com/ado/2007/08/dataservices');
  echo $properties->xpath('data:Id')[0], "\n";
  echo $properties->xpath('data:NEW_DATE')[0], "\n\n";
}

XPath is a syntax for defining parts of an XML document. The registerXPathNamespace() function creates a prefix for a specified namespace.

It makes sense to me why the atom prefix was created using registerXPathNamespace() for <entry xmlns="http://www.w3.org/2005/Atom"> in order to reference it like this //atom:entry/atom:content/meta:properties because the entry tag doesn't use a prefix.

Why was the meta prefix created? There was already an m: prefix with the same namespace defined.

Would using the below have done the same thing?

//atom:entry/atom:content/m:properties

Unfortunately, I don't have access to my server until later today. I'll test it myself then. If it doesn't work, more interested in why not, logic would suggest it should?


回答1:


The m: prefix was bound in the XML instance, but your XPath processor doesn't know that. It needs the prefix bound (registered) separately.

The meta: prefix is arbitrary. It could've been m: just like the XML, but it doesn't matter. As long as the URI is the same. The atom: prefix could've been a: and it would still work.




回答2:


Probably this would help you.Best guide on namespace that I found on the net.

https://www2.informatik.hu-berlin.de/~xing/Lib/NamespacesFAQ.htm#q8_5



来源:https://stackoverflow.com/questions/33809590/using-xml-prefixes-namespaces-to-create-an-xpath

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