Diferencia entre revisiones de «Gambas/Manipular documentos XML»

Contenido eliminado Contenido añadido
Sin resumen de edición
Línea 84:
 
== '''Usar XSLT''' ==
XSLT is a language for transforming XML documents into other XML documents. For instance, you can convert any XML into a HTML document or more complex task as create a PDF file. The XSLT template contains all the required instructions to transform a given XML document into other XML format.
You’ll create the following XSLT template to transform Heroes.XML into HTML format. Using a plain text editor write the following XSLT file and save it on the users home folder as xml2xhtml.XSLT.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<head>
<title>Example of XSL Transformation</title>
<style type="text/css">
table {padding: 2pt; widht:100%;}
th {font-weight: bold; background-color: #cccc99; color: #000000}
tr.odd {background-color: #ffffff; text-align: center}
tr.even {background-color: #f5f5dc; text-align: center}
</style>
</head>
<body>
<h1>Characters of the series: Heroes</h1>
<table>
<tr>
<th>Name</th>
<th>Played by</th>
<th>Ability</th>
</tr>
<xsl:for-each select="characters/*" >
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<tr class="even">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="played_by" /></td>
<td><xsl:value-of select="ability" /></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr class="odd">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="played_by" /></td>
<td><xsl:value-of select="ability" /></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Create a new console application with support for XML & XSLT. For Main() function write the following code:
DIM docXML AS NEW XmlDocument
DIM docXSLT AS NEW XmlDocument
DIM docXHTML AS NEW XmlDocument
 
docXML.Open(User.Home & "/Heroes.xml")
docXSLT.Open(User.Home & "/xml2xhtml.xslt")
docXHTML = Xslt.Transform(docXML, docXSLT)
docXHTML.Write(User.Home & "/Heroes.html")
Obviously, you can add any fancy error-handling code you want. Or proactively check if both files (Heroes.xml & xml2xhtml.xslt) exist, or if is enough space on disk to save the resultant XHTML file.
Here we’ll focus to understand how the XSLT template works, because as you just see the code in Gambas is pretty straight forward. The application open the two files Heroes.xml & xml2xhtml.xslt, then calls the Transform() method that perform the conversion to XHTML and finally save the file to the specified location.
I’m going to skip all HTML related and the XSL headers. The key element of the XSLT language you used is the for-each loop that will navigate thru all the elements arranged under the root tag characters. Inside the loop, the values of the elements name, played_by & ability are accessed using value-of. There is other item of the XSLT language used in this example: the choose-when-otherwise. This was used in order to apply some styling to the HTML report created from the XML document.
If you need to learn more about XML, why don’t you try [[http://www.w3.org/TR/xslt20/ http://www.w3.org/TR/xslt20/]]