Transformation de XSL
|
Vue d'ensemble :
|
Le diagramme repr�sente le processus de transformation : rentrant un document de XML, s'appliquant le dossier de XSLT aux noeuds du document de XML, et produire d'un autre document du format d�sir�.
|
|
Il se composent de deux fonctions. La premi�re fonction lit simplement un document de XML et transforme alors les donn�es en utilisant un dossier de XSLT. La deuxi�me fonction lit un document de XML, transforme les donn�es, et puis �crit un nouveau document de XML des donn�es transform�es.
|
Exemple 1 : - XML de transformation � XML
|
Transformation du document de XML
|
<?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>
|
Par l'application les XSLT transforment :
<?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>
|
R�sultats dans un nouveau document de XML, ayant une autre 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>
|
Exemple 2 : XML de transformation � XHTML
|
Exemple du document entrant de XML :
|
<?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>
|
Exemple 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 produit que ceci produirait
<?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.
|