本文介绍了我如何替换HXT中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个示例xml文件:

 < root> 
< tag attr =value>内容< / tag>
< tag attr =value2>内容< / tag>
< / root>

如何将每个标记替换为不同的标签,所以我得到不同的文件:

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

文档[1]似乎使用了过滤器,是否有一种方法可以单独使用箭头来完成此操作?




更新



我现在处于i可以替换像这样的节点:$ b​​
$ b

  runX $ readDocument []in.xml
>>> ; processTopDown(
(eelemtag2+ = sattrattr2XXX)
`when`(isElem>>> hasNametag))
>> > writeDocument []test.xml

但我不知道如何获取属性。




[1]

解决方案

从:

  runX $ readDocument []in .xml>>>转换>>> writeDocument []test.xml
其中
transform = processTopDown $
(setElemName(mkNametag2)>>>
processAttrl(changeAttrName $ mkName.attrMap 。localPart)
)`when`(isElem>>> hasNametag)
attrMapattr=attr2
attrMap a = a

这适用于我的示例文档。


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

解决方案

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.

这篇关于我如何替换HXT中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:28