Bonjour,
Cela fait plusieurs heures que je cherche (sans résultat parfait) à hiérarchiser mon fichier.
Voici mon fichier test :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
<body>
 
    <Intro>Element d'intro <span>01</span></Intro>
    <Intro>Element d'intro  02</Intro>
    <Intro>Element d'intro  03</Intro>
    <Intro>Element d'intro  04</Intro>
 
    <Preface>PREFACE</Preface>
    <p>Texte de préface ou autre</p>
 
    <h1>GROS TITRE A</h1>
        <p><span>Texte</span> sous le gros titre</p>
        <p>Texte sous le gros titre</p>
 
    <h2>SOUS TITRE A1</h2>
        <p>Texte <span>A1</span></p>
        <p>Texte A1</p>
        <p>Texte A1</p>
 
    <h2>SOUS TITRE A2</h2>
        <p>Texte A2</p>
        <p>Texte A2</p>
        <p>Texte A2</p>
 
    <h2>SOUS TITRE A3</h2>
        <p>Texte A3</p>
        <p>Texte A3</p>
        <p>Texte A3</p>
 
    <h1>GROS TITRE B</h1>
        <p>Texte B0</p>
 
    <h2>SOUS TITRE B1</h2>
        <p>Texte <span>B1</span></p>
        <p>Texte B1</p>
        <p>Texte B1</p>
 
</body>
Voici ce que j'aimerai obtenir :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
<body>
 
    <Intro>Element d'intro <span>01</span></Intro>
    <Intro>Element d'intro 02</Intro>
    <Intro>Element d'intro 03</Intro>
    <Intro>Element d'intro 04</Intro>
 
    <Preface>PREFACE</Preface>
    <p>Texte de préface ou autre</p>
 
    <div1>
        <h1>GROS TITRE A</h1>
        <p><span>Texte</span> sous le gros titre</p>
        <p>Texte sous le gros titre</p>
        <div2>
            <h2>SOUS TITRE A1</h2>
            <p>Texte <span>A1</span></p>
            <p>Texte A1</p>
            <p>Texte A1</p>
        </div2>
        <div2>
            <h2>SOUS TITRE A2</h2>
            <p>Texte A2</p>
            <p>Texte A2</p>
            <p>Texte A2</p>
        </div2>
        <div2>
            <h2>SOUS TITRE A3</h2>
            <p>Texte A3</p>
            <p>Texte A3</p>
            <p>Texte A3</p>
        </div2>
    </div1>
 
    <div1>
        <h1>GROS TITRE B</h1>
        <p>Texte B0</p>
        <div2>
            <h2>SOUS TITRE B1</h2>
            <p>Texte <span>B1</span></p>
            <p>Texte B1</p>
            <p>Texte B1</p>
        </div2>
    </div1>
</body>
Voici la transformation que j'ai écrite jusqu'à présent :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
 
    <xsl:template match="/">
        <header>
            <xsl:copy-of select="descendant::Intro"/>
        </header>
        <document>
            <xsl:copy-of select="descendant::Preface"/>
            <xsl:apply-templates select="descendant::h1"/>
        </document>
    </xsl:template>
 
    <xsl:template match="Intro"/>
 
    <xsl:template match="h1">
        <div1>
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>
            <xsl:if test="name(following-sibling::*[1])!='h1'">
                <xsl:apply-templates select="following-sibling::*[1]" mode="pas"/>
            </xsl:if>
        </div1>
    </xsl:template>
 
    <xsl:template match="h1" mode="pas"/>
 
    <xsl:template match="*" mode="pas">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::*[1]" mode="pas"/>
    </xsl:template>
 
    <xsl:template match="h2" mode="pas">
        <div2>
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>
            <xsl:if test="name(following-sibling::*[1])!='h2'">
                <xsl:apply-templates select="following-sibling::*[1]" mode="pas2"/>
            </xsl:if>
        </div2>
    </xsl:template>
 
    <xsl:template match="h2" mode="pas2"/>
 
    <xsl:template match="*" mode="pas2">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::*[1]" mode="pas2"/>
    </xsl:template>
 
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
 
</xsl:stylesheet>
Cependant, cette transformation pose problème :
- l.12 : la transformation ne fonctionne pas si je ne fais pas spécifiquement un <xsl:apply-templates select="descendant::h1"/>, ce qui m'oblige à appeler les autres éléments spécifiquement aussi dans mon <xsl:template match="/">, sauf que ceux-ci sont variables ;
- l.38 : <xsl:template match="h2" mode="pas"> ne fonctionne pas, mais je pense que je ne suis pas loin.

Pour l'instant j'arrive à ce résultat:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
<header>
   <Intro>Element d'intro <span>01</span></Intro>
   <Intro>Element d'intro  02</Intro>
   <Intro>Element d'intro  03</Intro>
   <Intro>Element d'intro  04</Intro>
</header>
<document>
   <Preface>PREFACE</Preface>
   <div1>
      <h1>GROS TITRE A</h1>
      <p>
         <span>Texte</span> sous le gros titre</p>
      <p>Texte sous le gros titre</p>
      <div2>
         <h2>SOUS TITRE A1</h2>
         <p>Texte <span>A1</span></p>
         <p>Texte A1</p>
         <p>Texte A1</p>
      </div2>
   </div1>
   <div1>
      <h1>GROS TITRE B</h1>
      <p>A0 A0 A0</p>
      <div2>
         <h2>SOUS TITRE B1</h2>
         <p>Texte <span>B1</span></p>
         <p>Texte B1</p>
         <p>Texte B1</p>
      </div2>
   </div1>
</document>
Merci pour l'aide éventuelle que vous pourriez m'apporter pour faire fonctionner correctement cette transformation !