Transformação de XSL
|
Vista geral:
|
O diagrama representa o processo da transformação: recolhendo um original de XML, aplicando a lima de XSLT aos nós do original de XML, e gerar um outro original do formato desejado.
|
|
Consiste em duas funções. A primeira função lê simplesmente um original de XML e transforma então os dados usando uma lima de XSLT. A segunda função lê um original de XML, transforma os dados, e escreve então um original novo de XML dos dados transformados.
|
Exemplo 1: - XML de transformação a XML
|
Transformando o original 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>
|
Aplicando-se os XSLT transformam:
<?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>
|
Resultados em um original novo de XML, tendo uma outra estrutura:
<?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>
|
Exemplo 2: XML de transformação a XHTML
|
Exemplo do original entrante 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>
|
Exemplo 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 que isto produziria
<?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.
|