简体   繁体   English

使用Ruby LibXML在Atom中找不到节点

[英]Can't find nodes in Atom with Ruby LibXML

I have the following XML Document: 我有以下XML文档:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:apps=\"http://schemas.google.com/apps/2006\" xmlns:gd=\"http://schemas.google.com/g/2005\">
  <apps:property name=\"memberId\" value=\"Tom\"/>
</atom:entry>

All I'm trying to do is get the apps:property node. 我要做的就是获取apps:property节点。 However everything I try in LibXML returns nothing. 但是,我在LibXML中尝试的所有操作均未返回任何结果。

From reading the LibXML documentation here for LibXML::XML::XPath. 此处阅读LibXML :: XML :: XPath的LibXML文档。 I would assume that I don't need to specify the two namespaces since they are defined on the root node. 我假设我不需要指定两个名称空间,因为它们是在根节点上定义的。

Here is what I've tried: 这是我尝试过的:

@document.find('/apps:property')
@document.find('//apps:property')
@document.find('/atom:entry/apps:property')
@document.find('//atom:entry/apps:property')
@document.find('/property')
@document.find('/atom:entry')
@document.find('//property')
@document.find('/entry')

None of the above return a result with the specified document. 以上都不返回指定文档的结果。 I've tried them all with different combinations of the atom and apps namespace definitions as well but it doesn't seem to have an effect. 我也尝试过使用原子和应用程序命名空间定义的不同组合对它们进行所有处理,但似乎没有效果。

If anyone could show me what very simple thing I must be missing it would be much appreciated. 如果有人能告诉我我必须错过的一件非常简单的事情,将不胜感激。


It turns out that the problem stems from the document being created dynamically rather than being read from a file. 事实证明,问题出在动态创建的文档而不是从文件中读取。

When I write the document to a file and read it in I can search it. 当我将文档写入文件并读入文件时,便可以进行搜索。 When I create the document, create the root element and add the child programmatically I get nil every time I try to search, oddly enough each works just fine. 当我创建文档时,创建根元素并以编程方式添加子元素,每次尝试搜索时,得到的结果都是nil,奇怪的是,每个元素都可以正常工作。 Do I have to "finalize" or do some other stupid thing to the document somehow before I can search it? 在搜索文档之前,是否必须对文档进行“定稿”或其他一些愚蠢的事情?

If I explicitly parse the document then I can search it. 如果我明确解析该文档,则可以对其进行搜索。

Looks like that might be the solution unless it's possible for the parse tree to be generated as the document is built. 除非在构建文档时可能生成解析树,否则看起来这可能是解决方案。

Time to look at the code I suppose. 是时候看一下我猜想的代码了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM