XSL Transformations: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
Riga 32:
 
Un documento XML può essere associato a più fogli di stile XSL, ciascuno dei quali genererà un output diverso. Lo stesso procedimento vale anche al contrario: uno stesso foglio di stile può essere applicato a più documenti XML, allo scopo di produrre documenti dal contenuto differente ma formato analogo.
 
==Esempi XSLT ==
Esempio di un documento XML in arrivo document<source lang="xml">
<?xml version="1.0" ?>
<persons>
<person username="JS1">
<name>John</name>
<family-name>Smith</family-name>
</person>
<person username="MI1">
<name>Morka</name>
<family-name>Ismincius</family-name>
</person>
</persons>
</source>
 
===Example 1 (trasformazione da XML a XML)===
Il foglio di stile XSLT stylesheetfornisce template per trasformare il documento XML:<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
 
<xsl:template match="/persons">
<root>
<xsl:apply-templates select="person"/>
</root>
</xsl:template>
 
<xsl:template match="person">
<name username="{@username}">
<xsl:value-of select="name" />
</name>
</xsl:template>
 
</xsl:stylesheet>
</source>
 
La sua valutazione risulta in un nuovo documento XML, avente una nuova struttura:
 
<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name username="JS1">John</name>
<name username="MI1">Morka</name>
</root>
</source>
 
===Example 2 (transforming XML to XHTML)===
Processing the following example XSLT file
<source lang="xml">
<?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" encoding="UTF-8"/>
 
<xsl:template match="/persons">
<html>
<head> <title>Testing XML Example</title> </head>
<body>
<h1>Persons</h1>
<ul>
<xsl:apply-templates select="person">
<xsl:sort select="family-name" />
</xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
 
<xsl:template match="person">
<li>
<xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/>
</li>
</xsl:template>
 
</xsl:stylesheet>
</source>
con in ingresso il documento XML mostra il risultato nel seguente [[XHTML]] ([[Whitespace]] è stato corretto per chiarezza):
<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>Testing XML Example</title> </head>
<body>
<h1>Persons</h1>
<ul>
<li>Ismincius, Morka</li>
<li>Smith, John</li>
</ul>
</body>
</html>
</source>
This XHTML generates the output below when rendered in a [[web browser]].
[[Image:xslt ex2.png|thumb|center|Rendered XHTML generated from an XML input file and an XSLT transformation.]]
 
In order for a [[web browser]] to be able automatically to apply an XSL transformation to an XML document on display, an XML stylesheet processing instruction can be inserted into XML. So, for example, if the stylesheet in Example 2 above were available as "example2.xsl", the following instruction could be added to the original incoming XML:<ref>{{cite web | url = http://www.w3.org/TR/xslt#section-Embedding-Stylesheets | title = XSL Transformations (XSLT) Version 1.0: W3C Recommendation&nbsp;– Embedding Stylesheets | date = 16 November 1999 | publisher = W3C | accessdate = 2009-01-06}}</ref>
 
<source lang="xml">
<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>
</source>
 
(In this example, <code>text/xsl</code> is technically incorrect according to the W3C specifications, but it is the only media type that is widely supported across current (2009) browsers.)
 
== Voci correlate ==