XSLT et namespaces
Exemple : Le 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
.
<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>
<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>
Syntaxe : XSL 1.0
En XSLT 1.0, il faut matcher le nom des éléments en utilisant explicitement les namespaces :
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">
Il faut que les noms d'éléments soient préfixés
Exemple :
<xsl:template match="toto:e1/toto:e2">
Exemple : Solution en XSLT 1.0
<?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>
<?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>
Exemple : Solution en XSLT 1.0 avec namespace cible
<?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>
<?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>
Syntaxe : XSLT 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">
.
Exemple : Solution en XSLT 2.0 avec namespace cible
<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>
<?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>