使用XML前缀/命名空间创建Xpath [英] Using XML prefixes/namespaces to create an Xpath

查看:96
本文介绍了使用XML前缀/命名空间创建Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的先前问题的后续文章.我想我跳到了最深层次,所以花了一些时间来充分了解XML名称空间.

来自

我知道使用m:前缀是为了将properties元素与其他文档中具有相同名称的元素区分开,以防合并此类文档. 我知道要使用前缀,必须为该前缀定义一个名称空间.这样的定义是使用XML名称空间属性(xmlns)完成的,语法为xmlns:prefix="URI". URI是统一资源标识符,它基本上是元素的来源. URI以前称为统一资源名称(URN),基本上是同一回事. /p>

现在看看我之前的问题的答案:

$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是用于定义XML文档各部分的语法. registerXPathNamespace()函数为指定的名称空间创建前缀.

对于我来说,为什么atom前缀是使用registerXPathNamespace() for <entry xmlns="http://www.w3.org/2005/Atom">创建的,以便像这样//atom:entry/atom:content/meta:properties一样引用它,这是因为入口标记未使用前缀.

为什么创建了meta前缀?已经有一个m:前缀,并且定义了相同的名称空间.

使用以下内容会做同样的事情吗?

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

不幸的是,直到今天晚些时候我才可以访问我的服务器.那我自己测试一下.如果它不起作用,那么对为什么不感兴趣,逻辑会建议它应该这样做?

解决方案

在XML实例中绑定了m:前缀,但是您的XPath处理器不知道这一点.它需要单独绑定(注册)前缀.

meta:前缀是任意的.就像XML一样,它可能是m:,但这并不重要.只要URI相同即可. atom:前缀可能是a:,并且仍然可以使用.

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?

解决方案

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.

这篇关于使用XML前缀/命名空间创建Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆