REXML - 无法断言 XML 响应中存在值
今天早上我在这里使用 REXML 获得了帮助,答案帮助我更多地了解了它。然而我遇到了另一个问题,似乎无法弄清楚。
我的回答是这样的:
<?xml version="1.0"?>
<file>
<link a:size="2056833" a:mimeType="video/x-flv" a:bitrate="1150000.0" a:height="240" a:width="320" rel="content.alternate"> https://link.com</link>
</file>
所以,我想做的是断言 a:mimeType 是 video/x-flv
这是我尝试过的:
xmlDoc = REXML::Document.new(xml)
assert_equal xmlDoc.elements().to_a("file/link['@a:mimeType']").first.text, 'video/x-flv'
以及:
assert xmlDoc.elements().to_a("file/link['@a:mimeType']").include? 'video/x-flv'
以及各种组合。我实际上收到了很多这样的链接,但我只关心其中一个是否有这个 mimeType。此外,某些链接没有 mimeType。
非常感谢任何帮助。
谢谢, 阿德里安
I got help here this morning using REXML and the answer helped me understand more about it. However I've encountered another problem and can't seem to figure it out.
My response is like this:
<?xml version="1.0"?>
<file>
<link a:size="2056833" a:mimeType="video/x-flv" a:bitrate="1150000.0" a:height="240" a:width="320" rel="content.alternate"> https://link.com</link>
</file>
So, what I want to do is assert that a:mimeType is video/x-flv
Here's what I have tried:
xmlDoc = REXML::Document.new(xml)
assert_equal xmlDoc.elements().to_a("file/link['@a:mimeType']").first.text, 'video/x-flv'
and also:
assert xmlDoc.elements().to_a("file/link['@a:mimeType']").include? 'video/x-flv'
and various combinations. I actually get lots of these links back but I only really care if one of them has this mimeType. Also, some of the links don't have mimeType.
Any help greatly appreciated.
Thanks,
Adrian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
text
正在检索元素的文本内容(标签之间的文本)。您想要访问“属性”。尝试查看任一链接是否具有正确的 mimeType,您可以转换元素数组
进入 mimeType 属性数组并检查它是否包含正确的值:
UPDATE
或者更简单,检查属性 mimeTypes 是否具有正确的值:
感谢您教我一些有关 XPath 的知识; -)
text
is retrieving for text content of elements (text between tags). You want to access an "attribute". TryTo see if either of the links has the correct mimeType, you can convert the array of elements
into an array of mimeType attributes and check if it contains the right value:
UPDATE
Or much simpler, check if there is an element with the attribute mimeTypes having the right value:
Thanks for teaching me something about XPath ;-)