XSL Umwandlung
|
�berblick:
|
Diagramm stellt den Umwandlungproze� dar: ein XML Dokument einlassen, die XSLT Akte an den Nullpunkten des XML Dokumentes und das Erzeugen eines anderen Dokumentes des gew�nschten Formats anwendend.
|
|
Es besteht aus zwei Funktionen. Die erste Funktion liest einfach ein XML Dokument und wandelt dann die Daten mit einer XSLT Akte um. Die zweite Funktion liest ein XML Dokument, wandelt die Daten um und schreibt dann ein neues XML Dokument von den umgewandelten Daten.
|
Beispiel 1: - Umwandelndes XML zu XML
|
Umwandeln des XML Dokumentes
|
<?xml version="1.0"?>
<persons>
<person username="MP123456">
<name>John</name>
<family_name>Smith</family_name>
</person>
<person username="PK123456">
<name>Morka</name>
<family_name>Ismincius</family_name>
</person>
</persons>
|
Indem sie zutreffen, wandeln die XSLT um:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<transform>
<xsl:apply-templates/>
</transform>
</xsl:template>
<xsl:template match="person">
<record>
<username>
<xsl:value-of select="@username" />
</username>
<name>
<xsl:value-of select="name" />
</name>
</record>
</xsl:template>
</xsl:stylesheet>
|
Results in a new XML document, having another structure:
<?xml version="1.0" encoding="UTF-8"?>
<transform>
<record>
<username>MP123456</username>
<name>John</name>
</record>
<record>
<username>PK123456</username>
<name>Morka</name>
</record>
</transform>
|
Beispiel 2: Umwandelndes XML zu XHTML
|
Beispiel des ankommenden XML Dokumentes:
|
<?xml version="1.0" encoding="UTF-8"?>
<domains>
<sun.com ownedBy="Sun Microsystems Inc.">
<host>
www
<use>World Wide Web site</use>
</host>
<host>
java
<use>Java info</use>
</host>
</sun.com>
<w3.org ownedBy="The World Wide Web Consortium">
<host>
www
<use>World Wide Web site</use>
</host>
<host>
validator
<use>web developers who want to get it right</use>
</host>
</w3.org>
</domains>
|
Example XSLT Stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<!--XHTML document outline-->
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test1</title>
<style type="text/css">
h1 { padding: 10px; padding-width: 100%; background-color: silver }
td, th { width: 40%; border: 1px solid silver; padding: 10px }
td:first-child, th:first-child { width: 20% }
table { width: 650px }
</style>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!--Table headers and outline-->
<xsl:template match="domains/*">
<h1><xsl:value-of select="@ownedBy"/></h1>
<p>The following host names are currently in use at
<strong><xsl:value-of select="local-name(.)"/></strong>
</p>
<table>
<tr><th>Host name</th><th>URL</th><th>Used by</th></tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<!--Table row and first two columns-->
<xsl:template match="host">
<!--Create variable for 'url', as it's used twice-->
<xsl:variable name="url" select=
"normalize-space(concat('http://', normalize-space(node()), '.',
local-name(..)))"/>
<tr>
<td><xsl:value-of select="node()"/></td>
<td><a href="{$url}"><xsl:value-of select="$url"/></a></td>
<xsl:apply-templates select="use"/>
</tr>
</xsl:template>
<!--'Used by' column-->
<xsl:template match="use">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
|
XHTML output that this would produce
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta content="text/html;charset=UTF-8"
http-equiv="Content-Type" />
<title>test1</title>
<style type="text/css">
h1
{ padding: 10px; padding-width: 100%; background-color: silver }
td, th { width:
40%; border: 1px solid silver; padding: 10px }
td:first-child, th:first-child { width: 20%
}
table {
width: 650px }
</style>
</head>
<body>
<h1>Sun Microsystems Inc.</h1>
<p>The following host names are currently in use at <strong>sun.com</strong></p>
<table>
<tr>
<th>Host name</th>
<th>URL</th>
<th>Used by</th>
</tr>
<tr>
<td>www</td>
<td><a href="http://www.sun.com">http://www.sun.com</a></td>
<td>World Wide Web
site</td>
</tr>
<tr>
<td>java</td>
<td><a href="http://java.sun.com">http://java.sun.com</a></td>
<td>Java info</td>
</tr>
</table>
<h1>The World Wide Web Consortium</h1>
<p>The following host names are currently in use at
<strong>w3.org</strong></p>
<table>
<tr>
<th>Host name</th>
<th>URL</th>
<th>Used by</th>
</tr>
<tr>
<td>www</td>
<td><a href="http://www.w3.org">http://www.w3.org</a></td>
<td>World Wide Web site</td>
</tr>
<tr>
<td>validator</td>
<td><a href="http://validator.w3.org">http://validator.w3.org</a></td>
<td>web developers who want to get it
right</td>
</tr>
</table>
</body>
</html>
|
|
|
Keywords xslt transform,
xsl transform,
xslt transform xml,
xml xsl transform,
xalan transform,
xsl transforms,
c# xsl transform,
net xsl transform,
xslt transforms,
javascript xsl transform,
transform xml with xsl,
java xsl transform,
transform xml using xslt,
net xslt transform,
java xslt transform,
xsl identity transform,
xslt for each,
asp net xslt,
asp net transform,
xslt transformation.
|