我正在努力使用 haskell 删除一个元素及其所有子元素。 任务是从给定的 xml 文档中删除所有表标签(也许我不理解游标的概念,或者我缺少其他东西)。
我尝试了三种不同的方法:
xml-conduitxml-lensghc-8.0.1test.xml)/输出 INPUT EXPECTED OUTPUT (for the filtered cases)
<?xml version="1.0"?> | <?xml version="1.0"?>
<root> | <root>
<a> | <a>
... | ...
</a> | </a>
<b> | <b>
<table> | <bb>
<!--table entries--> | ...
</table> | </bb>
<bb> | </b>
... | <c>
</bb> | <cc>
</b> | ...
<c> | </cc>
<cc> | </c>
... | </root>
</cc>
</c>
</root>
{-# LANGUAGE OverloadedStrings #-}
module Minimal where
import Control.Lens
import Data.Conduit.Text as CT
import Data.Default
import qualified Data.Text.Lazy.IO as TIO
import Text.XML
import Text.XML.Cursor
import qualified Text.XML.Lens as L
import Data.Maybe (isNothing, isJust)
main :: IO ()
main = do test <- Text.XML.readFile def "./test.xml"
pput $ filterDocument test
let cursor = fromDocument test
pput $ docUp $ elemUp $ getRoot ((head $ cursor $// checkName (== "table")) {child = []} )
pput $ docUp $ elemUp $ filterChildren (checkName (/= "table")) cursor
return ()
filterChildren :: Axis -> Cursor -> Cursor
filterChildren pred c = c {child = map (filterChildren pred) (c $/ pred) }
filterDocument :: Document -> Document
filterDocument doc = doc & (L.root.L.entire.filtered (\e -> isJust $ e^?L.named "table") .~ emptyElemt)
where emptyElemt = Element "empty" mempty []
-- helper functions --
docUp :: Element -> Document
docUp e = Document {documentRoot = e, documentPrologue = Prologue [] Nothing [], documentEpilogue = [] }
elemUp :: Cursor -> Element
elemUp cursor = Element {elementName = "DOC", elementAttributes = mempty , elementNodes = [node cursor]}
elemUp' :: [Cursor] -> Element
elemUp' cursors = Element {elementName = "DOC", elementAttributes = mempty , elementNodes = map node cursors}
getRoot :: Cursor -> Cursor
getRoot c = let p = (c $| parent)
in if null p then c else getRoot $ head p
pput :: Document -> IO ()
pput = TIO.putStrLn . renderText pretty
where pretty = def {rsPretty = True}
> stack ghci
. . .
Ok, modules loaded: Minimal.
λ > main
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>
...
</a>
<b>
<empty>
<!-- table entries -->
</empty>
<bb>
...
</bb>
</b>
<c>
<cc>
...
</cc>
</c>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<DOC>
<root>
<a>
...
</a>
<b>
<table>
<!-- table entries -->
</table>
<bb>
...
</bb>
</b>
<c>
<cc>
...
</cc>
</c>
</root>
</DOC>
<?xml version="1.0" encoding="UTF-8"?>
<DOC>
<root>
<a>
...
</a>
<b>
<table>
<!-- table entries -->
</table>
<bb>
...
</bb>
</b>
<c>
<cc>
...
</cc>
</c>
</root>
</DOC>
最佳答案
这段代码似乎可以根据 xml-conduit 执行您想要的操作。我是从yesod网书开始的example并通过一个简单的递归函数实现了转换。
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Map as M
import Prelude hiding (readFile, writeFile)
import Text.XML
main :: IO ()
main = do
Document prologue root epilogue <- readFile def "test.xml"
let root' = transform root
writeFile def
{ rsPretty = True
} "output.html" $ Document prologue root' epilogue
transform :: Element -> Element
transform (Element _name attrs children) =
Element _name attrs (filterChildren children)
filterChildren :: [Node] -> [Node]
filterChildren = concatMap kickTable
where
kickTable :: Node -> [Node]
kickTable (NodeElement (Element "table" attrs children)) = -- Drop it
[ ]
kickTable (NodeElement (Element n attrs children)) = -- Recurse on
[ NodeElement (Element n attrs (filterChildren children)) ]
kickTable n = -- ok - whatever
[ n ]
我的 lens-foo 不够强大,无法说明为什么您的解决方案不起作用,但从文档中可以看出 - 您必须小心 filtered 不要违反遍历法则,尽管我不这样做当你违反它们时,我不知道会发生什么。
希望对您有所帮助。
关于xml - haskell:xml过滤子树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661098/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我有一个名为Post的类,我需要能够适应以下场景:如果用户选择了一个类别,则只显示该类别的帖子如果用户选择了一种类型,则只显示该类型的帖子如果用户选择了一个类别和类型,则只显示该类别中该类型的帖子如果用户没有选择任何内容,则显示所有帖子我想知道我的Controller是否不可避免地会因大量条件语句而显得粗糙...这是我解决此问题的错误方法-有谁知道我如何才能做到这一点?classPostsController 最佳答案 您最好遵循“胖模型,瘦Controller”的惯例,这意味着您应该将这种逻辑放在模型本身中。Post类应该能够报告
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
我仍然收到标题中的“错误”消息,但不知道如何解决。在ApplicationController中,classApplicationController在routes.rb#match'set_activity_account/:id/:value'=>'users#account_activity',:as=>:set_activity_account--thisdoesn'tworkaswell..resources:usersdomemberdoget:action_a,:action_bendcollectiondoget'account_activity'endend和User
对于用户模型,我有一个过滤器来检查用户的预订状态,该状态由整数值(0、1或2)表示。UserActiveAdmin索引页上的过滤器是通过以下代码实现的:filter:booking_status,as::select然而,这会导致下拉选项为0、1或2。当管理员用户从下拉列表中选择它们时,我更愿意自己将它们命名为“未完成”、“待定”和“已确认”之类的名称。有没有办法在不改变booking_status在模型中的表示方式的情况下做到这一点? 最佳答案 假设booking_status是模型中的枚举字段,您可以使用:过滤器:booking
我想禁用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