How do i replace Nodes in HXT?

天大地大妈咪最大 提交于 2019-12-07 01:15:42

问题


Given a sample xml file:

<root>
  <tag attr="value">Content</tag>
  <tag attr="value2">Content</tag>
</root>

how do i replace every tag with a different tag so i get a different file:

<root>
  <tag2 attr2="value"/>
  <tag2 attr2="value2"/>
</root>

The documentation [1] seems to use Filters, is there a way to accomplish this with arrows alone?


Update

i am now at the point where i can replace a node like this:

runX $ readDocument [] "in.xml" 
       >>> processTopDown( 
               (eelem "tag2" += sattr "attr2" "XXX" ) 
               `when` (isElem >>> hasName "tag") ) 
       >>> writeDocument [] "test.xml"

but i have no idea on how to get the attribute right.


[1] http://www.haskell.org/haskellwiki/HXT#Transform_external_references_into_absolute_reference


回答1:


Try setElemName, processAttrl, and changeAttrName from Text.XML.HXT.XmlArrow:

runX $ readDocument [] "in.xml" >>> transform >>> writeDocument [] "test.xml"
  where
    transform = processTopDown $
      ( setElemName (mkName "tag2") >>>
        processAttrl (changeAttrName $ mkName . attrMap . localPart)
      ) `when` (isElem >>> hasName "tag")
    attrMap "attr" = "attr2"
    attrMap a = a

This works for me with your sample document.



来源:https://stackoverflow.com/questions/9483248/how-do-i-replace-nodes-in-hxt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!