Bonjour à tous,

Je cherche à ajouter une balise à l'intérieur d'un élément, à un endroit précis d'une chaîne de caractères.

Voici mon fichier XML :

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
 
<DICTIONNAIRE>
 
<article id="DT05-00005" pg="1">
      <vedette>
         <sm>Abeil (L'),</sm>
      </vedette>
      <definition>
         <typologie>bois</typologie>, <localisation>commune de Saint-Michel-de-Chaillol</localisation>.</definition>
  </article>
 
  <article id="DT05-00006" pg="1">
      <vedette>
         <sm>Abeilles (Les),</sm>
      </vedette>
      <definition>
         <typologie>écart</typologie>, <localisation>commune d'Aiguilles</localisation>.</definition>
  </article>
 
</DICTIONNAIRE>
Dans <localisation>, je voudrais ajouter une balise <commune> autour du nom de la commune (ici donc, Saint-Michel-de-Chaillol et Aiguilles par exemple).

Voici l'XSLT que j'ai fait:

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
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0">
 
    <xsl:output 
        method="xml" 
        indent="yes"/>
 
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
 
    <xsl:template match="definition/localisation[1]">
 
        <xsl:copy>
 
            <xsl:apply-templates select="node() | @*"/>
 
            <xsl:choose>
 
                <xsl:when test="contains(., 'commune de')">
                    <xsl:element name="commune" >
                        <xsl:attribute name="precision">
                            <xsl:text>certain</xsl:text>
                        </xsl:attribute>
                        <xsl:value-of select="substring-after(., 'commune de ')"/>
                    </xsl:element>
                </xsl:when>
 
                <xsl:when test="contains(., 'commune d''')">
                    <xsl:element name="commune">
                        <xsl:attribute name="precision">
                            <xsl:text>certain</xsl:text>
                        </xsl:attribute>
                        <xsl:value-of select="substring-after(., 'commune d''')"/>
                    </xsl:element>
                </xsl:when>
 
            </xsl:choose>
 
        </xsl:copy>
    </xsl:template>    
</xsl:stylesheet>
Et j'obtiens 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
 
<DICTIONNAIRE>
 
  <article id="DT05-00005" pg="1">
      <vedette>
         <sm>Abeil (L'),</sm>
      </vedette>
      <definition>
         <typologie>bois</typologie>, <localisation>commune de Saint-Michel-de-Chaillol<commune precision="certain">Saint-Michel-de-Chaillol</commune>
         </localisation>.</definition>
  </article>
 
  <article id="DT05-00006" pg="1">
      <vedette>
         <sm>Abeilles (Les),</sm>
      </vedette>
      <definition>
         <typologie>écart</typologie>, <localisation>commune d'Aiguilles<commune precision="certain">Aiguilles</commune>
         </localisation>.</definition>
  </article>
 
</DICTIONNAIRE>
Si j'ai bien le nom de la commune dans la balise, il est en fait répété alors que je souhaiterais baliser le texte déjà présent dans mon élément <localisation>.

Comment puis-je faire ?

Merci d'avance pour votre aide !