...">

How to edit certain elements in an XML file using HXT?

In short, here is what I want to do:

"foo.xml":

<?xml version="1.0"?>
<foo>
  <bar>
    <baz>
      <a>foo</a>
      <a>bar</a>
      <a>baz</a>
    </baz>
  </bar>
</foo>

expected result (content "bar.xml"):

<?xml version="1.0"?>
<foo>
  <bar>
    <baz>
      <a>foo!</a>
      <a>bar!</a>
      <a>baz!</a>
    </baz>
  </bar>
</foo>

... my attempt to approach the problem:

module Main (main) where

import Control.Monad

import Control.Arrow.ArrowTree
import Text.XML.HXT.Core

main :: IO ()
main = void . runX $ readDocument [] "foo.xml" >>>
       applic >>> writeDocument [withIndent yes] "bar.xml"

applic :: IOSArrow XmlTree XmlTree
applic = getChildren >>> hasName "foo"
--       ^^ because of extra root node (?)
         /> hasName "bar" /> hasName "baz" /> hasName "a" >>> changeText excl

excl :: String -> String
excl = (++ "!")

Question: How to directly edit only selected elements without changing / deleting their root elements? Also note that this program does not create the file "bar.xml", so something is definitely wrong. Tracing shows that after applying the arrow, the applicdocument consists of three elements a( "foo", "bar"and "baz"; without exclamation points).

+4
source share
1 answer

, HXT, , , , . - HXT, .

, HXT wiki process*, processTopDown processChildren, . , , . , , . , , , processChildren HXT when, Control.Monad, . ,

applic
    = processChildren
    $ flip when (isElem >>> hasName "foo")
        $ processChildren
        $ flip when (isElem >>> hasName "bar")
            $ processChildren
            $ flip when (isElem >>> hasName "baz")
                $ processChildren
                $ flip when (isElem >>> hasName "a")
                    $ processChildren
                $ flip when isText
                    $ changeText excl

, . :

-- Fixity is important here, must be right-associative.
infixr 5 />/
(/>/) :: ArrowXml a => String -> a XmlTree XmlTree -> a XmlTree XmlTree
name />/ action
    = processChildren
    $ action `when` (isElem >>> hasName name)

applic = "foo" />/ "bar" />/ "baz" />/ "a" />/
    processChildren (
        changeText excl `when` isText
    )

processChildren , , a .

+2

Source: https://habr.com/ru/post/1583834/


All Articles