XML est plutôt pratique pour sa représentation d'arboressence selon une architecture en profondeur.
Cependant, il est parfois utile de passer d'un modèle à l'autre.
Les règles de transformation sont les suivantes :
- Chaque section devient un enfant de la section de niveau inférieur qui la précède immédiatement
- Chaque noeud texte devient enfant de la section qui le précède immédiatement
- Les attributs titre sont recopiés tels quels (ils ne sont là que pour faciliter la compréhension ; leur contenu est arbitraire)
Dans le cas présent, il faut à tout prix que le niveau de la section soit précisé, le script ne peut pas le détecter par lui-même.
Le XML |
<? xml version="1.0" encoding="ISO-8859-1"? >
< document >
< section niveau = " 1 " titre = " 1 " / >
< texte > aaaa< / texte >
< section niveau = " 2 " titre = " 1.1 " / >
< texte > bbbb< / texte >
< section niveau = " 3 " titre = " 1.1.1 " / >
< texte > cccc< / texte >
< section niveau = " 3 " titre = " 1.1.2 " / >
< texte > dddd< / texte >
< section niveau = " 2 " titre = " 1.2 " / >
< texte > eeee< / texte >
< section niveau = " 3 " titre = " 1.2.1 " / >
< texte > ffff< / texte >
< section niveau = " 2 " titre = " 1.3 " / >
< texte > gggg< / texte >
< texte > hhhh< / texte >
< section niveau = " 3 " titre = " 1.3.1 " / >
< section niveau = " 4 " titre = " 1.3.1.1 " / >
< texte > iiii< / texte >
< section niveau = " 4 " titre = " 1.3.1.2 " / >
< texte > jjjj< / texte >
< section niveau = " 1 " titre = " 2 " / >
< texte > kkkk< / texte >
< section niveau = " 2 " titre = " 2.1 " / >
< texte > llll< / texte >
< section niveau = " 2 " titre = " 2.2 " / >
< texte > mmmm< / texte >
< / document >
|
Le XSLT | <? xml version="1.0" encoding="UTF-8"? >
< xsl : stylesheet version = " 1.0 " xmlns : xsl = " http://www.w3.org/1999/XSL/Transform " xmlns : fo = " http://www.w3.org/1999/XSL/Format " >
< xsl : output method = " xml " indent = " yes " / >
< xsl : template match = " /document " >
< xsl : copy >
< xsl : apply-templates select = " section[@niveau = 1] " / >
< / xsl : copy >
< / xsl : template >
< xsl : template match = " section " >
< xsl : variable name = " id_noeud " select = " generate-id(.) " / >
< xsl : copy >
< xsl : copy-of select = " @titre " / >
< xsl : apply-templates select = " following-sibling::texte[generate-id(preceding-sibling::section[1]) = $id_noeud] " / >
< xsl : apply-templates select = " following-sibling::section[(@niveau = current()/@niveau + 1) and (generate-id(preceding-sibling::section[@niveau = current()/@niveau][1]) = $id_noeud)] " / >
< / xsl : copy >
< / xsl : template >
< xsl : template match = " texte " >
< xsl : copy-of select = " . " / >
< / xsl : template >
< / xsl : stylesheet >
|
Le Résultat |
<? xml version="1.0" encoding="UTF-8"? >
< document >
< section titre = " 1 " >
< texte > aaaa< / texte >
< section titre = " 1.1 " >
< texte > bbbb< / texte >
< section titre = " 1.1.1 " >
< texte > cccc< / texte >
< / section >
< section titre = " 1.1.2 " >
< texte > dddd< / texte >
< / section >
< / section >
< section titre = " 1.2 " >
< texte > eeee< / texte >
< section titre = " 1.2.1 " >
< texte > ffff< / texte >
< / section >
< / section >
< section titre = " 1.3 " >
< texte > gggg< / texte >
< texte > hhhh< / texte >
< section titre = " 1.3.1 " >
< section titre = " 1.3.1.1 " >
< texte > iiii< / texte >
< / section >
< section titre = " 1.3.1.2 " >
< texte > jjjj< / texte >
< / section >
< / section >
< / section >
< / section >
< section titre = " 2 " >
< texte > kkkk< / texte >
< section titre = " 2.1 " >
< texte > llll< / texte >
< / section >
< section titre = " 2.2 " >
< texte > mmmm< / texte >
< / section >
< / section >
< / document >
|
|