haskell xml使用HXT库更新文本 [英] haskell xml update text using HXT library

查看:58
本文介绍了haskell xml使用HXT库更新文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有可能更新<node><data key="attr">text</data></node>这样的结构中的文本.有什么办法可以通过在Haskell中使用HXT库吗?

I need to have a possibility to update text in structure like this <node><data key="attr">text</data></node>. Is there any way to do this by using HXT library in haskell?

推荐答案

自然的答案是镜头库.我认为hxt没有与之相关的东西.但是有xml-lens使用xml-conduit(实际上不是管道)

The natural answer is a lens library; I don't think hxt has such a thing associated with it. But there is xml-lens which uses xml-conduit (but not conduits in fact)

自述文件中的示例 https://github.com/fumieval/xml-lens 非常简单,但如果您不熟悉镜头,可能会有点操作员阻塞.但是这里是例子之一的较不密集版本.通过在书单中添加页数"并更改

The examples in the readme https://github.com/fumieval/xml-lens are fairly straightforward, but maybe a bit operator-clogged, if you aren't familiar with lenses. But here is less dense version of one of the examples. It modifies each page number in a book list by adding " pages" to it, changing

  <pages>360</pages>

  <pages>360 pages</pages>

以这样的XML开头:

<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book category="Textbooks">
    <title>Learn You a Haskell for Great Good!</title>
    <author year="2011">Miran Lipovaca</author>
    <pages>360</pages>
</book>
<book category="Textbooks">
    <title>Programming in Haskell</title>
    <author year="2007">Graham Hutton</author>
    <pages>200</pages>
</book>
</books>

我们需要像这样的进口

  >>> import Text.XML.Lens        -- from the lens-xml package
  >>> import Control.Lens         -- from the lens package
  >>> import Text.XML             -- from the xml-conduit package
  >>> import Data.Monoid ((<>))
  >>> import qualified Data.Text as T
  >>> import qualified Data.ByteString.Lazy.Char8 as BL
  >>> :set -XOverloadedStrings    -- for Text literals

首先,我定义针对我要更改的遍历:

First I define the traversal that targets what I want to change:

  >>> let my_focus = root . el "books" ./ el "book" ./ el "pages" . text

然后我用over some_traversal some_function

  >>> 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

,然后对其进行变换和渲染:

and then transform and render it:

  >>> 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>

考虑到它在地下使用的幻想装置,这可能会有点慢,但显然很强大.

This might be a little slow given the fancy apparatus it is using below the surface, but is plainly crazy powerful.

这篇关于haskell xml使用HXT库更新文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆