Comment remplacer les nœuds dans HXT ?
Question
Étant donné un exemple de fichier XML :
<root>
<tag attr="value">Content</tag>
<tag attr="value2">Content</tag>
</root>
comment puis-je remplacer chaque tag
avec une balise différente donc j'obtiens un fichier différent :
<root>
<tag2 attr2="value"/>
<tag2 attr2="value2"/>
</root>
La documentation [1] semble utiliser des filtres, existe-t-il un moyen d'y parvenir avec des flèches uniquement ?
Mise à jour
j'en suis maintenant au point où je peux remplacer un nœud comme celui-ci :
runX $ readDocument [] "in.xml"
>>> processTopDown(
(eelem "tag2" += sattr "attr2" "XXX" )
`when` (isElem >>> hasName "tag") )
>>> writeDocument [] "test.xml"
mais je n'ai aucune idée sur la façon d'obtenir le bon attribut.
[1] http://www.haskell.org/haskellwiki/HXT#Transform_external_references_into_absolute_reference
La solution
Essayer setElemName
, processAttrl
, et changeAttrName
depuis 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
Cela fonctionne pour moi avec votre exemple de document.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow