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?