XSLT et namespaces

ExempleLe problème

Soit le fichier XML et un programme XSLT associé. La transformation ne fonctionne pas comme prévue, car la règle essaye de matcher document, alors que le nom développé est document.fr:document.

CTRL+C pour copier, CTRL+V pour coller
1
<?xml-stylesheet href="doc.xsl" type="text/xsl"?>
2
<document xmlns="document.fr">
3
    <texte>Mon texte</texte>
4
</document>
<?xml-stylesheet href="doc.xsl" type="text/xsl"?>
<document xmlns="document.fr">
    <texte>Mon texte</texte>
</document>
CTRL+C pour copier, CTRL+V pour coller
1
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
2
    <xsl:template match="document">
3
        <html>
4
            <body><p><xsl:value-of select="texte"/></p></body>
5
        </html>
6
    </xsl:template>
7
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="document">
        <html>
            <body><p><xsl:value-of select="texte"/></p></body>
        </html>
    </xsl:template>
</xsl:stylesheet>

SyntaxeXSL 1.0

En XSLT 1.0, il faut matcher le nom des éléments en utilisant explicitement les namespaces :

  1. Il faut déclarer l'espace de nom dans la feuille XSLT

    Exemple : <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:toto="toto.fr">

  2. Il faut que les noms d'éléments soient préfixés

    Exemple : <xsl:template match="toto:e1/toto:e2">

ExempleSolution en XSLT 1.0

CTRL+C pour copier, CTRL+V pour coller
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="document.fr">
3
    <xsl:template match="d:document">
4
        <html>
5
            <body><p><xsl:value-of select="d:texte"/></p></body>
6
        </html>
7
    </xsl:template>
8
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="document.fr">
    <xsl:template match="d:document">
        <html>
            <body><p><xsl:value-of select="d:texte"/></p></body>
        </html>
    </xsl:template>
</xsl:stylesheet>

ExempleSolution en XSLT 1.0 avec namespace cible

CTRL+C pour copier, CTRL+V pour coller
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="document.fr">
3
    <xsl:template match="d:document">
4
        <html xmlns="http://www.w3.org/1999/xhtml">
5
            <body>
6
                <p><xsl:value-of select="d:texte"/></p>
7
            </body>
8
        </html>
9
    </xsl:template>
10
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="document.fr">
    <xsl:template match="d:document">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <body>
                <p><xsl:value-of select="d:texte"/></p>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

SyntaxeXSLT 2.0

En XSLT 2.0, l'attribut xpath-default-namespace permet de spécifier le namespace par défaut d'une XSLT et évite ainsi de préfixer tous les éléments :

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="toto.fr">.

ExempleSolution en XSLT 2.0 avec namespace cible

CTRL+C pour copier, CTRL+V pour coller
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
3
    xpath-default-namespace="document.fr">
4
    <xsl:template match="document">
5
        <html xmlns="http://www.w3.org/1999/xhtml">
6
            <body>
7
                <p><xsl:value-of select="texte"/></p>
8
            </body>
9
        </html>
10
    </xsl:template>
11
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xpath-default-namespace="document.fr">
    <xsl:template match="document">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <body>
                <p><xsl:value-of select="texte"/></p>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Rappel