Content Content how to ...">

How to replace nodes in HXT?

Given a sample XML file:

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

how to replace each tag with a different tag to get a different file:

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

The documentation [1] apparently uses filters, is there a way to do this with arrows alone?


Update

Now I am at the point where I can replace the node as follows:

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

but I don’t know how to get the attribute correctly.


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

+7
source share
1 answer

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.

+2
source

All Articles