我有以下示例 xml:
<data>
<products>
<product>
<section>Red Section</section>
<images>
<image>img.jpg</image>
<image>img2.jpg</image>
</images>
</product>
<product>
<section>Blue Section</section>
<images>
<image>img.jpg</image>
<image>img3.jpg</image>
</images>
</product>
<product>
<section>Green Section</section>
<images>
<image>img.jpg</image>
<image>img2.jpg</image>
</images>
</product>
</products>
</data>
我知道如何在 Clojure 中解析它
(require '[clojure.xml :as xml])
(def x (xml/parse 'location/of/that/xml'))
这将返回一个描述 xml 的嵌套映射
{:tag :data,
:attrs nil,
:content [
{:tag :products,
:attrs nil,
:content [
{:tag :product,
:attrs nil,
:content [] ..
这个结构当然可以用标准的 Clojure 函数遍历,但它可能会变得非常冗长,尤其是与,例如,用 XPath 查询它相比。是否有任何助手可以遍历和搜索这种结构?我怎样才能,例如
<product> 的列表<images> 的产品标签包含 <image>带有文本“img2.jpg”section的产品是“红区”谢谢
最佳答案
使用 Zippers来自 data.zip这是您的第二个用例的解决方案:
(ns core
(:use clojure.data.zip.xml)
(:require [clojure.zip :as zip]
[clojure.xml :as xml]))
(def data (zip/xml-zip (xml/parse PATH)))
(def products (xml-> data :products :product))
(for [product products :let [image (xml-> product :images :image)]
:when (some (text= "img2.jpg") image)]
{:section (xml1-> product :section text)
:images (map text image)})
=> ({:section "Red Section", :images ("img.jpg" "img2.jpg")}
{:section "Green Section", :images ("img.jpg" "img2.jpg")})
关于xml - 在 Clojure 中搜索 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11537923/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在搜索我的值是方法的散列时遇到问题。我只是不想运行plan_type与键匹配的方法。defmethod(plan_type,plan,user){foo:plan_is_foo(plan,user),bar:plan_is_bar(plan,user),waa:plan_is_waa(plan,user),har:plan_is_har(user)}[plan_type]end目前如果我传入“bar”作为plan_type,所有方法都会运行,我怎么能只运行plan_is_bar方法呢? 最佳答案 这个变体怎么样?defmethod
我想获取主题名称与搜索关键字匹配的所有配置文件。现在我正在加载所有配置文件。我需要知道如何实现它。非常感谢任何帮助。配置文件.rbhas_many:categorizationshas_many:subjects,through::categorizations主题.rbhas_many:categorizationshas_many:profiles,through::categorizations分类.rbbelongs_to:profilebelongs_to:subjectviews/search/index.html.erb#searchform'get'do%>nil%>#
我正在编写一个简单的日志嗅探器,它将在日志中搜索表明我支持的软件存在问题的特定错误。它允许用户指定日志路径并指定他们想要搜索多少天前。如果用户关闭日志滚动,日志文件有时会变得非常大。目前我正在做以下事情(虽然还没有完成):File.open(@log_file,"r")do|file_handle|file_handle.eachdo|line|ifline.match(/\d+++-\d+-\d+/)etc...line.match显然会查找我们在日志中使用的日期格式,其余逻辑将在下面。但是,有没有更好的方法来搜索没有.each_line的文件?如果没有,我完全同意。我只是想确保我使
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::
我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att
请原谅我的无知,我是Ruby新手。我知道如何使用正则表达式搜索字符串,甚至单个文件:str=File.read('example.txt')match=str.scan(/[0-9A-Za-z]{8,8}/)putsmatch[1]我知道如何在多个文件和目录中搜索静态短语pattern="hello"Dir.glob('/home/bob/**/*').eachdo|file|nextunlessFile.file?(file)File.open(file)do|f|f.each_linedo|line|puts"#{pattern}"ifline.include?(pattern)e
操作系统:CentOS6.2x86_64很抱歉缩进太古怪了。这是我的第一篇SO帖子,我是新来设置服务器的。不过,我正在学习,并将详细说明我尝试解决此问题所采取的步骤以及寻求帮助的地方。我是一位有抱负的年轻Web开发人员,并且我在其他人配置的服务器上工作,因此,这对我来说是全新的。我正在准备我最近购买的用于运行Rails应用程序的linode。我遵循了此处http://blog.blenderbox.com/2011/01/07/installing-rvm-ruby-rails-passenger-nginx-on-centos/提供的初始安装指南,并更改了步骤:sudobash反射(