Falando sobre XSLT
Posted by Pontilhado | Posted in Outras, XML, XSLT | Posted on 24-08-2009
Tags: CSS, HTML, XML, XSLT
0
Sejam Bem-vindos ao Blog do Pontilhado.
Escolhi esse primeiro assunto como pontapé inicial do Blog. Aproveitei uma semana de estudo sobre essa linguagem para falar um pouco sobre ela!
Observação
Antes de estudar XSLT você deve saber o básico de XML e XML Namespaces.
Resumo
XSLT, é uma linguagem para transformar documentos XML em outros documentos XML.
XSLT é projetado para ser utilizado como parte de XSL (eXtensible Stylesheet Language), que é um estilo de linguagem XML. Além de XSLT, XSL inclui um vocabulário XML para a especificação de formatação. XSL especifica o estilo de um documento XML, utilizando XSLT para descrever como o documento é transformado em um outro XML que usa o vocabulário de formatação.
XSL consiste de 3 partes:
- XSLT é uma linguagem para transformar documentos XML
- XPath é uma linguagem para definir partes de um documento XML
- XSL-FO é uma linguagem para formatar documentos XML
Pense em XSL como um conjunto de linguagens que podem transformar XML em XHTML, filtrar e ordenar dados XML, definir partes de um documento XML, formatar dados XML baseados nos valores dos dados, como mostrar valores negativos em vermelho, e exportar dados XML para mídias diferentes, como telas, papel ou voz.
XSLT tornou-se uma Recomendação W3C em 16 de Novembro de 1999.
Para conhecer mais afundo eu recomento essa apostila da Visie que encontrei XSLT.
Abaixo um exemplo simples e outro mais avançado:
Documento XML (Simples)
< ?xml version="1.0" encoding="utf-8"?> < ?xml-stylesheet type="text/xsl" href="menu.xsl"?> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> </catalog>
Documento XSLT (Simples)
< ?xml version="1.0" encoding="utf-8"?> <xsl :stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> </xsl><xsl :template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl :for-each select="catalog/cd"> <tr> <td> <xsl :value-of select="title"/> </td> <td> <xsl :value-of select="ARTIST"/> </td> </tr> </xsl> </table> </body> </html> </xsl>
Documento XML (avançado)
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="menu.xsl"?> <menu> <secao id="1" name="menu-esportes"> <titulo url="link.html" title="Esportes">Esportes</titulo> <item url="link.html" title="Vôlei">Vôlei</item> <item url="link.html" title="Futebol">Futebol</item> <item url="link.html" title="Fórmula 1">Fórmula 1</item> </secao> <secao id="2" name="menu-noticias"> <titulo>Notícias</titulo> <item url="link.html" title="volei">Rio de Janeiro</item> <item url="link.html" title="volei">São Paulo</item> <item url="link.html" title="volei">Tecnologia</item> </secao> <secao id="3" name="menu-entretenimento"> <titulo>Entretenimento</titulo> <item url="link.html" title="volei">Rio de Janeiro</item> <item url="link.html" title="volei">São Paulo</item> <item url="link.html" title="volei">Tecnologia</item> <select id="select-tvglobo" name="telejornaisProgramas"> <item value="link.html">Alexandre</item> <item value="link.html">Alexandre2</item> <item value="link.html">Alexandre3</item> <item value="link.html">Alexandre4</item> </select> </secao> </menu>
Documento XSLT (Avançado)
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="menu.xsl"?> <menu> <secao id="1" name="menu-esportes"> <titulo url="link.html" title="Esportes">Esportes</titulo> <item url="link.html" title="Vôlei">Vôlei</item> <item url="link.html" title="Futebol">Futebol</item> <item url="link.html" title="Fórmula 1">Fórmula 1</item> </secao> <secao id="2" name="menu-noticias"> <titulo>Notícias</titulo> <item url="link.html" title="volei">Rio de Janeiro</item> <item url="link.html" title="volei">São Paulo</item> <item url="link.html" title="volei">Tecnologia</item> </secao> <secao id="3" name="menu-entretenimento"> <titulo>Entretenimento</titulo> <item url="link.html" title="volei">Rio de Janeiro</item> <item url="link.html" title="volei">São Paulo</item> <item url="link.html" title="volei">Tecnologia</item> <select id="select-tvglobo" name="telejornaisProgramas"> <item value="link.html">Alexandre</item> <item value="link.html">Alexandre2</item> <item value="link.html">Alexandre3</item> <item value="link.html">Alexandre4</item> </select> </secao> </menu>
Documento XSLT (Avançado)
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>menu</title> <style> *{margin:0;padding:0;} ul, li {list-style-type:none;padding:0;margin:0;} legend, fieldset {border:0;} legend {display:none;} h2 {margin-top:15px;} select {display:none;} .on {display:block;} .menu-esportes a, .menu-esportes {color:#006600} .menu-noticias a, .menu-noticias {color:#FF0000} .menu-entretenimento a, .menu-entretenimento {color:#ef630c} </style> <script type="text/javascript" src="script.js"></script> </head> <body> <h2>Menu</h2> <div id="glb-menu"> <xsl:apply-templates/> <script type="text/javascript">glbMenu();</script> </div> </body> </html> </xsl:template> <xsl:template match="secao"> <h2><xsl:apply-templates select="titulo"/></h2> <ul> <xsl:apply-templates select="item"/> </ul> <xsl:apply-templates select="select"/> </xsl:template> <xsl:template match="titulo"> <xsl:choose> <xsl:when test="@url and @title" > <a href="{@url}" title="{@title}"><xsl:value-of select="." /></a> </xsl:when> <xsl:otherwise> <xsl:value-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="item"> <li><a href="{@url}" title="{@title}"><xsl:value-of select="."/></a></li> </xsl:template> <xsl:template match="select"> <div id="{@id}"> <form action="" method=""> <fieldset> <legend>Acesso rápido à links sobre globo news</legend> <xsl:element name="select"> <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute> <xsl:for-each select="item"> <xsl:element name="option"> <xsl:attribute name="value"> <xsl:value-of select="@value"/> </xsl:attribute> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> </xsl:element> </fieldset> </form> </div> </xsl:template> </xsl:stylesheet>
