使用HXT库更新haskell xml文本
我需要有一个可能性来更新结构中的文本,比如使用HXT库更新haskell xml文本,xml,haskell,Xml,Haskell,我需要有一个可能性来更新结构中的文本,比如text。使用haskell中的HXT库有什么方法可以做到这一点吗?自然的答案是镜头库;我认为《代码》hxt《代码》中没有这样的东西。但是有xml镜头,它使用xml导管(但实际上不是导管) 自述文件中的示例相当简单,但如果您不熟悉镜头,可能会有一点操作员阻塞。但这里是其中一个例子的不太密集的版本。它通过添加“页面”来修改图书列表中的每个页码,更改 <pages>360</pages> 然后我用它定义了一个普通的Haskell
text
。使用haskell中的HXT库有什么方法可以做到这一点吗?自然的答案是镜头库;我认为《代码》hxt《代码》中没有这样的东西。但是有xml镜头
,它使用xml导管
(但实际上不是导管)
自述文件中的示例相当简单,但如果您不熟悉镜头,可能会有一点操作员阻塞。但这里是其中一个例子的不太密集的版本。它通过添加“页面”来修改图书列表中的每个页码,更改
<pages>360</pages>
然后我用它定义了一个普通的Haskell函数,使用遍历某个函数
>>> let my_transformation = over my_focus (<> " pages") -- i.e. apply (<> " pages") to
>>> :t my_transformation -- all the focused positions
my_transformation :: Document -> Document
>>让my_transformation=覆盖my_焦点(“页面”)——即应用(“页面”)到
>>>:t my_变换--所有聚焦位置
my_transformation::Document->Document
阅读文件:
>>> doc <- Text.XML.readFile def "book.xml"
>>> :t doc
doc :: Document
>>doc>>:t doc
文件
然后对其进行变换和渲染:
>>> BL.putStrLn $ renderLBS def (my_transformation doc)
<?xml version="1.0" encoding="UTF-8"?><books>
<book category="Textbooks">
<title>Learn You a Haskell for Great Good!</title>
<author year="2011">Miran Lipovaca</author>
<pages>360 pages</pages>
</book>
<book category="Textbooks">
<title>Programming in Haskell</title>
<author year="2007">Graham Hutton</author>
<pages>200 pages</pages>
</book>
</books>
>>BL.putStrLn$renderLBS def(我的转换文档)
向你学习哈斯克尔语是非常有益的!
米兰脂囊
360页
Haskell编程
格雷厄姆·赫顿
200页
鉴于stackoverflow在地下使用的奇特设备,这可能有点慢,但显然是疯狂的强大。嗨,欢迎来到stackoverflow-这是一个平台,人们可以免费回答问题并互相帮助。所以,请在你的问题上投入至少和你期望有人回答一样多的努力。你可以告诉我们你尝试过什么,伟大的搜索引擎告诉过你什么或没有。您可以参考您不理解的文档,或者您通常被困在哪里。现在,我将根据你的努力给出一个答案:“我认为这是可能的。”
>>> let my_focus = root . el "books" ./ el "book" ./ el "pages" . text
>>> let my_transformation = over my_focus (<> " pages") -- i.e. apply (<> " pages") to
>>> :t my_transformation -- all the focused positions
my_transformation :: Document -> Document
>>> doc <- Text.XML.readFile def "book.xml"
>>> :t doc
doc :: Document
>>> BL.putStrLn $ renderLBS def (my_transformation doc)
<?xml version="1.0" encoding="UTF-8"?><books>
<book category="Textbooks">
<title>Learn You a Haskell for Great Good!</title>
<author year="2011">Miran Lipovaca</author>
<pages>360 pages</pages>
</book>
<book category="Textbooks">
<title>Programming in Haskell</title>
<author year="2007">Graham Hutton</author>
<pages>200 pages</pages>
</book>
</books>