XSLT

Start

XML

Namespace

XSL

DTD

Schema

Standardisierung

IMS LD

EML

LMML

Akronyme

Quellen

 

 

XSL: Templates ] [ XSLT ] XSL-FO ] XPath ]   Bsp.: [ XML-Buchliste ] [ value-of ] [ for-each ] [ apply-templates ] [ XPath ]

Extensible Stylesheet Language Transformations (XSLT) wird unter http://www.w3.org/TR/xslt wie folgt beschrieben:"... XSLT, which is a language for transforming XML documents into other XML documents".

Mit Hilfe von XSLT können XML Dokumente in andere Datenformate transformiert werden. Folgende Abbildung zeigt den Ablauf einer XSLT Transformation:

Folgendes Beispiel zeigt die Transformation in eine HTML Datei.

Das XML Dokument (xslt/buchliste_little.xml):

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="xsl_html.xsl"?>
<buchliste>
	<buch>
		<titel>Uli Stein´s Tierleben</titel>
		<autor>
			<vorname>Uli</vorname>
			<nachname>Stein</nachname>
		</autor>
		<ausgabe>Taschenbuch</ausgabe>
		<seiten>45</seiten>
		<preis>9,2 Euro</preis>
	</buch>
	<buch>
		<titel>Alles Liebe!</titel>
		<autor>
			<vorname>Uli</vorname>
			<nachname>Stein</nachname>
		</autor>
		<ausgabe>Taschenbuch</ausgabe>
		<seiten>23</seiten>
		<preis>11,3 Euro</preis>
	</buch>
	<buch>
		<titel>Die Alpen</titel>
		<autor>
			<vorname>Heinz</vorname>
			<nachname>Berg</nachname>
		</autor>
		<ausgabe>Taschenbuch</ausgabe>
		<seiten>323</seiten>
		<preis>41,3 Euro</preis>
	</buch>
</buchliste>

Die transformierende XSL Datei (xslt/xsl_html.xsl)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
   <xsl:template match="/">
	<html>
	<head><title>Buchliste mit XSL in HTML Transformiert</title>
	</head>
	<body>
	  <h3>Buchbeschreibung</h3>
	   <xsl:for-each select="buchliste/buch">
      		<span style="font-weight:bold">Autor: </span>
      		<xsl:value-of select="autor"/><br/>
      		<span style="font-weight:bold">Titel: </span>
      		<xsl:value-of select="titel"/><br/>
      		<span style="font-weight:bold">Preis: </span>
      		<xsl:value-of select="preis"/><br/>
      		<span style="font-weight:bold">Ausgabe: </span>
      		<xsl:value-of select="ausgabe"/><br/>
      		<span style="font-weight:bold">Seitenzahl: </span>
      		<xsl:value-of select="seiten"/><br/><br/>
	   </xsl:for-each>
	</body>
	</html>
   </xsl:template>
</xsl:stylesheet>

Ohne for-each wird nur das erste Element angezeigt. Zusätzlich müsste es ohne for-each lauten <xsl:value-of select="buchliste/buch/autor"/>. Der Netscape zeigt obigen Quelltext als Ergebnis alle Bücher in einer Zeile. <span> bewirkt keinen Zeilenumbruch, <div> schon.

Das Endergebnis als HTML Datei (xslt/html_output.htm):

<html>

<head>
<title>Buchliste mit XSL in HTML Transformiert</title>
</head>

<body>

<h3>Buchbeschreibung</h3>
<span style="font-weight:bold">Autor: </span>Uli Stein<br />
<span style="font-weight:bold">Titel: </span>Uli Stein´s Tierleben<br />
<span style="font-weight:bold">Preis: </span>9,2 Euro<br />
<span style="font-weight:bold">Ausgabe: </span>Taschenbuch<br />
<span style="font-weight:bold">Seitenzahl: </span>45<br /><br />

<span style="font-weight:bold">Autor: </span>Uli Stein<br />
<span style="font-weight:bold">Titel: </span>Alles Liebe!<br />
<span style="font-weight:bold">Preis: </span>11,3 Euro<br />
<span style="font-weight:bold">Ausgabe: </span>Taschenbuch<br />
<span style="font-weight:bold">Seitenzahl: </span>23<br /><br />

<span style="font-weight:bold">Autor: </span>Heinz Berg<br />
<span style="font-weight:bold">Titel: </span>Die Alpen<br />
<span style="font-weight:bold">Preis: </span>41,3 Euro<br />
<span style="font-weight:bold">Ausgabe: </span>Taschenbuch<br />
<span style="font-weight:bold">Seitenzahl: </span>323<br /><br />

</body>

</html>

Packt man das ganze mit Hilfe von XSL in eine Tabelle sieht die XSL Datei (xslt/xsl_html_tabelle.xsl)  wie folgt aus:

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- ein nbsp wird als entität nicht erkannt -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
	<xsl:template match="/">
		<html>
			<head>
				<title>Buchliste mit XSL in HTML Transformiert</title>
			</head>
			<body>
				<h3>Buchbeschreibung</h3>
				<table border="0" cellpadding="2">
					<xsl:for-each select="buchliste/buch">
						<tr>
							<td>Autor:</td>
							<td>
								<xsl:value-of select="autor"/>
							</td>
						</tr>
						<tr>
							<td>Titel:</td>
							<td>
								<xsl:value-of select="titel"/>
							</td>
						</tr>
						<tr>
							<td>Preis:</td>
							<td>
								<xsl:value-of select="preis"/>
							</td>
						</tr>
						<tr>
							<td>Ausgabe:</td>
							<td>
								<xsl:value-of select="ausgabe"/>
							</td>
						</tr>
						<tr>
							<td>Seitenzahl:</td>
							<td>
								<xsl:value-of select="seiten"/>
							</td>
						</tr>
						<tr>
							<td><br/></td>
							<td><br/></td>						
						</tr>
					</xsl:for-each>
				</table>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Das Endergebnis mit Tabelle zeigt die xslt/buchliste_little_tabelle.xml. Über height in einer td kann die Zeilenhöhe genau definiert werden.

Serverseitige XML-Unterstützung

Will man serverseitig mit Hilfe von XSLT eine XML-Datei z. B. in HTML transformieren, stehen die verschiedensten serverseitigen Programmiersprachen zur Verfügung. Das Endergebnis der Transformation wird in Form einer HTML-Datei zum Client geschickt. Folgende Quelltexte (xsl/xslt/xsl_html.asp, ) zeigen die Umsetzung in ASP (Active Server Pages mit Visual Basic Script) und PHP. Ob die XML-Unterstützung durch PHP geleistet wird, kann mit der

<?php
phpinfo();
?>

Funktion geprüft werden.


ASP-Variante:

Quelltext der asp-Datei (xsl_html.asp) zur Transformation einer XML-Datei mit Hilfe einer XSLT-Datei:

<%
'Instanz eines XML-Parsers erstellen und Laden der XML-Datei in den Arbeitsspeicher
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("buchliste_little.xml"))

'Weitere Instanz eines XML-Parsers erstellen und Laden der XSL-Datei in den Arbeitsspeicher
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("xsl_html.xsl"))

'Transformation der XML-Datei mit Hilfe der XSL-Datei, Rückgabe des Ergebnisses an den Browser
Response.Write(xml.transformNode(xsl))
%>

PHP-Variante:

Für die XML-Unterstützung muss PHP nicht eigens konfiguriert werden. Folgende Anweisung zeigen ein Installations- und Anwendungs-Beispiel unter Linux und Windows:

Linux (Installation Apache 2 und PHP 5, im YAST2 können die verschiedenen Module ebenfalls konfiguriert werden):

  1. Man sollte während der Installation als root angemeldet sein, da ansonsten Zugriffsrechte entsprechend gesetzt werden müssen.
  2. Die Installation ist sehr gut unter http://www.php.net/manuel/de/install.apache2.php beschrieben (siehe unten).

http://www.php.net/manuel/de/install.apache2.php: Beispiel 3-5. Installation Instructions (Apache 2 Shared Module Version)

1.  gzip -d httpd-2_0_NN.tar.gz
2.  tar xvf httpd-2_0_NN.tar
3.  gunzip php-NN.tar.gz
4.  tar -xvf php-NN.tar
5.  cd httpd-2_0_NN
6.  ./configure --enable-so
7.  make
8.  make install
 
    Now you have Apache 2.0.NN available under /usr/local/apache2,
    configured with loadable module support and the standard MPM prefork.
    To test the installation use your normal procedure for starting
    the Apache server, e.g.:
    /usr/local/apache2/bin/apachectl start
    and stop the server to go on with the configuration for PHP:
    /usr/local/apache2/bin/apachectl stop.
 
9.  cd ../php-NN
 
10. Now, configure your PHP.  This is where you customize your PHP
    with various options, like which extensions will be enabled.  Do a
    ./configure --help for a list of available options.  In our example
    we'll do a simple configure with Apache 2 and MySQL support.  Your
    path to apxs may differ, in fact, the binary may even be named apxs2 on
    your system. 
    
      ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
    
    Damit mysql Unterstützung "mit installiert" werden kann, muss zuerst 
    überhaupt ein mysql-client installiert werden.
11. make
12. make install
 
    If you decide to change your configure options after installation,
    you only need to repeat the last three steps. You only need to
    restart apache for the new module to take effect. A recompile of
    Apache is not needed.
                
    Note that unless told otherwise, 'make install' will also install PEAR,
    various PHP tools such as phpize, install the PHP CLI, and more
    (auch XML Unterstützung).
    
13. Setup your php.ini 
    
    cp php.ini-dist /usr/local/lib/php.ini
          
    You may edit your .ini file to set PHP options.  If you prefer having
    php.ini in another location, use --with-config-file-path=/some/path in
    step 10.
    
    If you instead choose php.ini-recommended, be certain to read the list
    of changes within, as they affect how PHP behaves.
 
14. Edit your httpd.conf to load the PHP module.  The path on the right hand
    side of the LoadModule statement must point to the path of the PHP
    module on your system.  The make install from above may have already
    added this for you, but be sure to check.
 
    For PHP 4:
  
      LoadModule php4_module libexec/libphp4.so
      
    For PHP 5 (ist automatisch schon passiert):
    
      LoadModule php5_module libexec/libphp5.so
 
15. Tell Apache to parse certain extensions as PHP.  For example,
    let's have Apache parse the .php extension as PHP.  You could
    have any extension(s) parse as PHP by simply adding more, with
    each separated by a space.  We'll add .phtml to demonstrate.
    Die rot markierte Zeile muss hinzugefügt warden.
            
      AddType application/x-httpd-php .php .phtml
                  
    It's also common to setup the .phps extension to show highlighted PHP
    source, this can be done with:
    
      AddType application/x-httpd-php-source .phps
 
16. Use your normal procedure for starting the Apache server, e.g.:
   
      /usr/local/apache2/bin/apachectl start

Windows (Installation PHP 5):

Die Installation von PHP5 für IIS unter Windows XP Professional geht leicht von der Hand. Bitte unter http://www.php.net/downloads.php PHP 5.0.0 installer per Download holen und installieren. Eine andere Variante um u.a. Apache, MySQL und PHP (4 und 5) unter Windows und Linux im Paket zu installieren findet sich unter http://www.apachefriends.org/de/xampp.html . Zusätzlich kann Java Tomcat und Cocoon installiert werden.

PHP 4 und XML

Bei folgender Fehlermeldung (beim Aufruf einer php-Datei, die eine XML-Datei mit Hilfe einer XSLT-Datei transformieren soll) ist die Pfadangabe zur XML- oder XSL-Datei falsch. Unter Windows und unter Linux reicht der Dateiname, solange die XML- und XSLT-Dateien im rootweb des Webservers liegen! Die php-Datei kann in einem Subweb liegen.

Warning: Sablotron error on line 1: XML parser error 4: not well-formed (invalid token) in c:\inetpub\wwwroot\php\list.php on line 8
An error occurred: XML parser error 4: not well-formed (invalid token)(error code 2)

Lösung (funktioniert unter Windows und Linux, alle Dateien liegen im rootweb des Webservers), Quelltext der php-Datei (xsl_html.php) zur Transformation einer XML-Datei mit Hilfe einer XSLT-Datei:

<?php
// Dateinamen der XML- und XSL-Datei angeben
$xml_datei = "buchliste_little.xml";
$xslt_datei = "xsl_html.xsl";

// XSLT-Prozessor erstellen
$xslt_prozessor = xslt_create() or die("Der XSLT-Prozessor konnte nicht erstellt werden!");

// Transformation der XML-Datei mit Hilfe der XSLT-Datei, Ergebnis liegt in ergebnis
if($ergebnis = xslt_process($xslt_prozessor, $xml_datei, $xslt_datei))
{
	// Ergebnis im Browser ausgeben
	echo $ergebnis;
}
else
{
	// falls ein Fehler auftritt, Error-Meldung ausgeben
	echo "Folgender Fehler trat auf: " . xslt_error($xslt_prozessor) . "(Fehlernummer " . xslt_errno($xslt_prozessor) . ")";
}

// Freigabe der Ressourcen, die durch den XSLT-Prozessor-Handler belegt wurden
xslt_free($xslt_prozessor);
?>

PHP 5 DOM und XML

<?php

$dom = new domDocument();
  $dom->load("xsl_html.xsl");
  $xslt_proc = new xsltprocessor;
  $xsl = $xslt_proc->importStylesheet($dom);
  
  $document = new DomDocument();
  $document->load("buchliste_little.xml");
  print $xslt_proc->transformToXml($document);
  
  ?>

Beispiele: