element-available()-Funktion | |
Diese Funktion legt fest, ob ein bestimmtes Element für den XSLT-Prozessor verfügbar sein soll. Diese Funktion erlaubt Ihnen, Stylesheets zu entwerfen, die auch funktionsfähig sind, wenn zum Verarbeiten eines XML-Dokuments ein bestimmtes Element nicht verfügbar ist. | |
Eingaben | |
Der Name des Elements. Es sollte ein qualifizierter Name mit Namensraum sein. Stimmt der Namensraum-URI mit dem XSLT-Namensraum-URI überein, dann verweist der Elementname auf ein durch XSLT definiertes Element. Andernfalls verweist der Name auf ein Erweiterungselement. Besitzt der Elementname einen Null-Namensraum-URI, gibt die Funktion element-available den Wert false zurück. |
|
Ausgabe | |
Der Boolesche Wert true, wenn das Element verfügbar ist, andernfalls false. |
|
Definition | |
XSLT-Abschnitt 15, Rückgriff |
|
Beispiel | |
Das nachfolgende Beispiel dient dazu, die Funktion element-available() zu erproben: <?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> Hier das Stylesheet: <?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> Dieses Stylesheet versucht, Teile des Inhalts der XML-Datei in verschiedene HTML-Dateien zu schreiben. Das erste <chapter>-Element wird in die Datei Wenn Sie Xalan dafür verwenden, die XML-Datei ohne dieses Stylesheet zu verarbeiten, erhalten Sie die folgenden Ergebnisse auf der Konsole: file:///D:/O'Reilly/XSLT/bookSamples/AppendixC/elementavailable.xsl; Line 66; Column 35; The <write> element is not available! Das Stylesheet erzeugt die Dateien <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> Wie das HTML-Dokument in einem Browser angezeigt wird, sehen Sie in Abbildung C-1. Beispiel-HTML-Ausgabedatei Wenn der Anwender auf den Link Previous klickt, wird die Datei Wird das Stylesheet mit Saxon verwendet (mit Hilfe des Befehls java com.icl.saxon.StyleSheet chapterlist.xml elementavailable.xsl), werden auf der Konsole ähnliche Ergebnisse erzeugt: The <write> element is not available! Obwohl das Format der Nachricht sich etwas unterscheidet, ist die Ausgabe in den mehrfachen HTML-Dateien dieselbe. Schließlich wird der Oracle-XML-Parser verwendet. Keins der abgefragten Elemente wird verfügbar sein, die gesamte Ausgabe wird daher in eine einzige Datei geschrieben. Der Prozessor wird mit dem nachfolgenden Befehl aufgerufen. (Der Befehl sollte in einer einzigen Zeile stehen.) java oracle.xml.parser.v2.oraxsl chapterlist.xml elementavailable.xsl chapters.html Hier die Ausgabe auf der Konsole: Message: The <write> element is not available! Die Ausgabedatei, <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> Wie die Ausgabe dargestellt wird, sehen Sie in Abbildung C-2. HTML-Dokument, das alle Kapitel (chapters) auflistet In diesem Beispiel können Sie mit der Funktion element-available() zum einen bestimmen, welche Verarbeitungsfähigkeiten verfügbar sind, und zum andern mit dem Vorgefundenen eine funktionsfähige Ausführung gewährleisten. |