XSL

XSL ist eine Programmiersprache zur Transformation von XML-Dokumenten. XSLT ist eine Subsprache von XSL, zusammen mit XSL-FO und XPath.

if, choose (else)


<xsl:if test="Bedingung">
		<!-- Anweisung -->
</xsl:if> 

<xsl:choose>
	<xsl:when test="Bedingung">
		<!-- Anweisung -->
	</xsl:when>
	<xsl:otherwise>
		<!-- sonst Anweisung -->
	</xsl:otherwise>
</xsl:choose>	
	

hello.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl" ?>
<sayHello>Hello World!</sayHello>	
	

hello.xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head></head>
			<body>
				<p align="center" style="font-size:32px; color:blue;">
					<xsl:value-of select="." />
				</p>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>	
	




warenkorb.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="warenkorb.xsl" ?>
<warenkorb>
	<artikel>
		<id>11</id>
		<art>Obst</art>
		<name>Apfel</name>
		<preis>5.85</preis>
	</artikel>
	<artikel>
		<id>22</id>
		<art>Gemüse</art>
		<name>Sellerie</name>
		<preis>4.98</preis>
	</artikel>
	<artikel>
		<id>33</id>
		<art>Obst</art>
		<name>Birne</name>
		<preis>6.45</preis>
	</artikel>
	<artikel>
		<id>44</id>
		<art>Gemüse</art>
		<name>Kartoffel</name>
		<preis>3.65</preis>
	</artikel>
</warenkorb>
	

warenkorb.xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:decimal-format name="euro" decimal-separator="," grouping-separator="."/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="warenkorb.css"/>
</head>
<body>
	<table style="margin-bottom: 10px;">
		<tr>
			<th class="tableHead" style="width: 30px;"> </th>
			<th class="tableHead" style="width: 30px;">ID</th>
			<th class="tableHead" style="width: 60px;">Art</th>
			<th class="tableHead" style="width: 110px;">Bezeichnung</th>
			<th class="tableHead" style="width: 80px;">Preis [€/kg]</th>
		</tr>
		<xsl:for-each select="warenkorb/artikel">
		<xsl:sort select="art"/>
		<xsl:sort select="name"/>
			<tr>
				<td class="tableBody colNmumber">
					<xsl:value-of select="position()" />
				</td>
				<td class="tableBody colNmumber">
					<xsl:value-of select="id" />
				</td>
				<td class="tableBody">
					<xsl:value-of select="art" />
				</td>
				<td class="tableBody">
					<xsl:value-of select="name" />
				</td>
				<td class="tableBody colNmumber">
					<xsl:value-of select="format-number(preis, '#.###,00', 'euro')"/>
				</td>
			</tr>
		</xsl:for-each>
			<tr>
				<td class="tableHead colNmumber" colspan="4">Summe:</td>
				<td class="tableBody colNmumber" style="font-weight: bold;">
				<xsl:value-of select="format-number(sum(warenkorb/artikel/preis), '#.###,00', 'euro')"/>
				</td>
			</tr>
	</table>

	<table class="tableAlone" style="float: left;">
		<xsl:call-template name="ArtikelHead">
			<xsl:with-param name="_textHead">Gemüse</xsl:with-param>
		</xsl:call-template>
		<xsl:for-each select="warenkorb/artikel">
		<xsl:sort select="name"/>
		<xsl:if test="art != 'Obst'">
			<xsl:call-template name="Artikel" />
		</xsl:if>
		</xsl:for-each>
	</table>	

	<table class="tableAlone">
		<xsl:call-template name="ArtikelHead">
			<xsl:with-param name="_textHead">Obst</xsl:with-param>
		</xsl:call-template>
		<xsl:for-each select="warenkorb/artikel">
		<xsl:sort select="name"/>
		<xsl:if test="art = 'Obst'">
			<xsl:call-template name="Artikel" />
		</xsl:if>
		</xsl:for-each>
	</table>		
			
</body>
</html>
</xsl:template>

<xsl:template name="ArtikelHead">
	<xsl:param name="_textHead" />
	<tr>
		<th class="tableHead" style="width: 30px;">ID</th>
		<th class="tableHead"><xsl:value-of select="$_textHead" /></th>
	</tr>
</xsl:template>

<xsl:template name="Artikel">
	<tr>
		<td class="tableBody colNmumber">
			<xsl:value-of select="id" />
		</td>
		<td class="tableBody">
			<xsl:value-of select="name" />
		</td>
	</tr>	
</xsl:template>
</xsl:stylesheet>
	

warenkorb.css


.tableHead {
  background-color: #89b0d1;
  color: white;
  font-size: 14px;
  font-weight: bold;
}
.tableBody {
  color: #4E4E4E;
  font-size: 12px;
}
.colNmumber {
	text-align: right;
}
.tableHead, .tableBody {
  font-family: "MS Serif", "New York", serif;
  border: 1px solid gray;
  padding: 4px 4px 4px 4px;
}	
.tableAlone {
	min-width: 140px;
}
	




attribute.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="attribute.xsl" ?>
<test>
	<text ausrichtung="links">
		linksbündiger Text
	</text>
	<text ausrichtung="zentriert">
		zentrierter Text
	</text>
	<text ausrichtung="rechts">
		rechtsbündiger Text
	</text>
</test>
	

attribute.xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head>
			</head>
			<body>
				<div style="width: 300px; border: 1px solid gray;">
					<xsl:apply-templates />
				</div>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="text">
		<p>
			<xsl:choose>
				<xsl:when test="@ausrichtung='zentriert'">
					<xsl:attribute name="align">center</xsl:attribute>
				</xsl:when>
				<xsl:when test="@ausrichtung='rechts'">
					<xsl:attribute name="align">right</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="align">left</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="." />
		</p>
	</xsl:template>
</xsl:stylesheet>
	




kontakt.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="kontakt.xsl" ?>
<kontakt>
	<adresse mail="max.mustermann@example.org">
		Max Mustermann
	</adresse>
	<adresse mail="maxi.mustermann@example.org">
		Stanislaw Ludowiczy
	</adresse>
</kontakt>
	

kontakt.xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head>
				<style type="text/css">
				.adresse, .email {
				  font-family: "MS Serif", "New York", serif;
				  padding: 4px 4px 4px 4px;
				}					
				.adresse { 			
				  width: 300px;
				  background-color: #89b0d1;
				  color: white;
				  font-size: 14px;
				  font-weight: bold;	
				  border: 1px solid gray; 
				  margin-top: 10px;
				}
				.email { 			
				  background-color: white;
				  color: black;
				  font-size: 12px;
				}				
				</style>
			</head>
			<body>
				<xsl:apply-templates />
			</body>
		</html>
	</xsl:template>

	<xsl:template name="Label">
		<xsl:param name="_email" />
		<div class="email">
			<xsl:text>E-Mail: </xsl:text>
			<a href="mailto:{$_email}"><xsl:value-of select="$_email" /></a>
		</div>
	</xsl:template>

	<xsl:template match="kontakt/adresse">
		<div class="adresse">
			<xsl:value-of select="." />
			<xsl:call-template name="Label">
				<xsl:with-param name="_email">
					<xsl:value-of select="@mail" />
				</xsl:with-param>
			</xsl:call-template>

		</div>
	</xsl:template>
</xsl:stylesheet>
	




schleife.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="schleife.xsl" ?>
<count>
	<start>10</start>
	<ende>15</ende>
</count>
	

schleife.xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
	<style type="text/css">
	.colBody {
	  min-width: 60px;
	  text-align: center;
	  font-family: "MS Serif", "New York", serif;
	  padding: 4px 4px 4px 4px;
	  border: 1px solid gray; 
	}			
	</style>			
</head>
<body>
	<table>
		<xsl:call-template name="schleife">
			<xsl:with-param name="_counter" select="number(/count/start)" />
		</xsl:call-template>
	</table>
</body>
</html>
</xsl:template>

<xsl:template name="schleife">
	<xsl:param name="_counter" />
	<xsl:if test="$_counter <= number(/count/ende)">
		<tr>
			<td class="colBody">
				<xsl:value-of select="$_counter" />
			</td>
			<td class="colBody">
				<xsl:value-of select="$_counter * $_counter" />
			</td>
		</tr>
		<xsl:call-template name="schleife">
			<xsl:with-param name="_counter" select="$_counter + 1" />
		</xsl:call-template>		
	</xsl:if>
</xsl:template>
</xsl:stylesheet>
	




Top


Example
Sitemap Kontakt Impressum