2

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?

Community
  • 1
  • 1
DVCITIS
  • 1,067
  • 3
  • 16
  • 36
  • Next question in my series on XML namespaces: http://stackoverflow.com/questions/33771883/name-sql-table-rows-to-match-xml-namespace-elements – DVCITIS Nov 19 '15 at 17:56

2 Answers2

3

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.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Great answer, thanks! How is your SQL? :) is this xPath approach the same if I was to try and match elements up with MySQL table rows? Have another Q: http://stackoverflow.com/questions/33771883/name-sql-table-rows-to-match-xml-namespace-elements – DVCITIS Nov 19 '15 at 17:37
  • Sorry @Pete my SQL experience is limited. – Daniel Haley Nov 19 '15 at 17:51
0

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

Mathews Mathai
  • 1,707
  • 13
  • 31