解析 RDF XML 文件以获取所有 rdf:about 值 [英] Parse RDF XML file to get all rdf:about values

查看:54
本文介绍了解析 RDF XML 文件以获取所有 rdf:about 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php 的简单 xml 和 xpath 来解析 rdf xml 文件,并且正在努力获取所有 rdf:about 值的列表.

I am using php's simple xml and xpath to parse an rdf xml file and am struggling to get a list of all the rdf:about values.

有什么建议吗?

推荐答案

在 PHP5.3 之前使用带有命名空间属性的 SimpleXml 时似乎存在问题.基本上,任何带有 : 的内容在转换为 SimpleXml 元素的对象属性时都会被删除.以下可以做,但对我来说感觉很黑:

There seems to be an issue when using SimpleXml with namespaced attributes prior to PHP5.3. Basically, anything with a : will be dropped when converted to an object property of a SimpleXml element. The following will do, but feels hackish to me:

$rdf = str_replace('rdf:about', 'rdf_about', $rdf);  
$rdf = new SimpleXMLElement($rdf);
foreach($rdf->xpath('//@rdf_about') as $node) {
  echo $node, PHP_EOL;
}

看这里:

您可以使用 DOM 代替 SimpleXml:

You could use DOM instead of SimpleXml:

$dom = new DomDocument;
$dom->loadXml($rdf);
$xph = new DOMXPath($dom);
$xph->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
foreach($xph->query('//@rdf:about') as $attribute) {
    echo $attribute->value, PHP_EOL;
}

但是,我建议通过 SimpleXml 或 DOM 使用专用库:

But, I suggest using a dedicated library for this over SimpleXml or DOM:

这里有一篇关于解析器的博文:

And here's a blog post about the parsers:

这篇关于解析 RDF XML 文件以获取所有 rdf:about 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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