解析 RDF XML 文件以获取所有 rdf:about 值

发布于 2024-08-26 04:16:02 字数 90 浏览 3 评论 0原文

我正在使用 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.

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2024-09-02 04:16:02

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

$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:

$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:

  • http://arc.semsol.org/docs/v2/parsing
  • < a href="http://www.seasr.org/wp-content/plugins/meandre/rdfapi-php/doc/" rel="noreferrer">http://www.seasr.org/wp-content/plugins /meandre/rdfapi-php/doc/
  • http://librdf.org/raptor/
  • < a href="http://phpxmlclasses.sourceforge.net/show_doc.php?class=class_rdf_parser.html" rel="noreferrer">http://phpxmlclasses.sourceforge.net/show_doc.php?class=class_rdf_parser.html< /a>

这是一篇关于解析器的博客文章:

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;
}

See here:

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;
}

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

And here's a blog post about the parsers:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文