Trasformazione di XSL
|
Descrizione:
|
Lo schema rappresenta il processo di trasformazione: contenendo un documento di XML, applicando la lima di XSLT ai nodi del documento di XML e generazione deun altro documento della disposizione voluta.
|
|
Consiste di due funzioni. La prima funzione legge semplicemente un documento di XML ed allora trasforma i dati usando una lima di XSLT. La seconda funzione legge un documento di XML, trasforma i dati ed allora redige un nuovo documento di XML dai dati trasformati.
|
Esempio 1: - XML di trasformazione a XML
|
Trasformazione del documento di 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>
|
Applicandosi i XSLT trasformano:
<?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>
|
Risultati in un nuovo documento di XML, avendo altra struttura:
<?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>
|
Esempio 2: XML di trasformazione a XHTML
|
Esempio del documento ricevuto di 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>
|
Esempio 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 prodotto che questo produrrebbe
<?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.
|