Fonction element-available() | |
Détermine si le processeur XSLT peut accéder à un élément donné. Cette fonction permet de concevoir des feuilles de style réagissant avec élégance si un élément particulier n'est pas disponible pour traiter un document XML. | |
Entrées | |
Le nom de l'élément. Ce nom doit être qualifié par un espace de noms ; si l'URI de l'espace de noms est le même que l'URI d'espace de noms XSLT, le nom de l'élément fait alors référence à un élément défini par XSLT. Sinon, le nom fait référence à un élément de l'extension. Si le nom de l'élément possède un URI d'espace de noms nul, la fonction element-available renvoie alors la valeur false. |
|
Sortie | |
La valeur booléenne true si l'élément est disponible ; la valeur false dans le cas contraire. |
|
Défini dans | |
XSLT section 15, Traitement de secours. |
|
Exemple | |
L'exemple suivant a permis de tester la fonction element-available() : <?xml version="1.0"?> <book> <title>XSLT</title> <chapter> <title>Getting Started</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>The Hello World Example</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>XPath</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Stylesheet Basics</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Branching and Control Elements</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Functions</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Creating Links and Cross-References</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Sorting and Grouping Elements</title> <para>If this chapter had any text, it would appear here.</para> </chapter> <chapter> <title>Combining XML Documents</title> <para>If this chapter had any text, it would appear here.</para> </chapter> </book> La feuille de style est la suivante : <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="redirect saxon"> <xsl:output method="html"/> <xsl:template match="/"> <xsl:choose> <xsl:when test="element-available('redirect:write')"> <xsl:for-each select="/book/chapter"> <redirect:write select="concat('chapter', position(), '.html')"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body> <h1><xsl:value-of select="title"/></h1> <xsl:apply-templates select="para"/> <xsl:if test="not(position()=1)"> <p> <a href="chapter{position()-1}.html">Previous</a> </p> </xsl:if> <xsl:if test="not(position()=last())"> <p> <a href="chapter{position()+1}.html">Next</a> </p> </xsl:if> </body> </html> </redirect:write> </xsl:for-each> </xsl:when> <xsl:when test="element-available('saxon:output')"> <xsl:for-each select="/book/chapter"> <saxon:output file="chapter{position()}.html"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body> <h1><xsl:value-of select="title"/></h1> <xsl:apply-templates select="para"/> <xsl:if test="not(position()=1)"> <p> <a href="chapter{position()-1}.html">Previous</a> </p> </xsl:if> <xsl:if test="not(position()=last())"> <p> <a href="chapter{position()+1}.html">Next</a> </p> </xsl:if> </body> </html> </saxon:output> </xsl:for-each> </xsl:when> <xsl:otherwise> <html> <head> <title><xsl:value-of select="/book/title"/></title> </head> <xsl:for-each select="/book/chapter"> <h1><xsl:value-of select="title"/></h1> <xsl:apply-templates select="para"/> </xsl:for-each> </html> </xsl:otherwise> </xsl:choose> <xsl:if test="not(element-available('write'))"> <xsl:message terminate="no"> The <write> element is not available! </xsl:message> </xsl:if> </xsl:template> <xsl:template match="para"> <p><xsl:apply-templates select="*|text()"/></p> </xsl:template> </xsl:stylesheet> Cette feuille de style tente de prendre le contenu du fichier XML et d'en réécrire des portions dans différents fichiers HTML. Le premier élément <chapter> est écrit dans le fichier Si on utilise Xalan pour traiter le fichier XML avec cette feuille de style, la console indique les résultats suivants : file:///D:/O'Reilly/XSLT/bookSamples/AppendixC/elementavailable.xsl; Line 66; Column 35; The <write> element is not available! La feuille de style génère les fichiers <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>XPath</title> </head> <body> <h1>XPath</h1> <p>If this chapter had any text, it would appear here.</p> <p><a href="chapter2.html">Previous</a></p> <p><a href="chapter4.html">Next</a></p> </body> </html> Une fois affiché dans un navigateur, le fichier ressemble à la Figure C-1. Exemple de fichier de sortie HTML Cliquer sur le lien Précédent vous amène au fichier L'utilisation de la feuille de style avec Saxon (commande java com.icl.saxon.StyleSheet chapterlist.xml elementavailable.xsl) donne des résultats similaires sur la console : The <write> element is not available! Bien que le format du message soit légèrement différent, la sortie dans les différents fichiers HTML est la même. Enfin, si on utilise l'analyseur Oracle XML, aucun des éléments appelés ne seront disponibles et toutes les sorties seront écrites dans un seul fichier. La commande suivante permet d'invoquer le processeur. (la commande doit tenir sur une seule ligne.) java oracle.xml.parser.v2.oraxsl chapterlist.xml elementavailable.xsl chapters.html La sortie sur la console est la suivante : Message: The <write> element is not available! Voici à quoi ressemble le fichier de sortie, <html xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect" xmlns:saxon="http://icl.com/saxon"> <head> <META http-equiv="Content-Type" content="text/html"> <title>XSLT</title> </head> <h1>Getting Started</h1> <p>If this chapter had any text, it would appear here.</p> <h1>The Hello World Example</h1> <p>If this chapter had any text, it would appear here.</p> <h1>XPath</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Stylesheet Basics</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Branching and Control Elements</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Functions</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Creating Links and Cross-References</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Sorting and Grouping Elements</h1> <p>If this chapter had any text, it would appear here.</p> <h1>Combining XML Documents</h1> <p>If this chapter had any text, it would appear here.</p> </html> Une fois affichée, la sortie ressemble à la Figure C-2. Document HTML répertoriant tous les chapitres Dans cet exemple, la fonction element-available() permet de déterminer les capacités de traitement disponibles et de réagir avec élégance quelque soit le résultat trouvé. |