<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Linea de Codigo &#187; XSLT</title> <atom:link href="http://lineadecodigo.com/categoria/xslt/feed/" rel="self" type="application/rss+xml" /><link>http://lineadecodigo.com</link> <description>/* Programación en la red */</description> <lastBuildDate>Tue, 07 Feb 2012 07:00:08 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Convertir un fichero XML en una página web</title><link>http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/</link> <comments>http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/#comments</comments> <pubDate>Fri, 01 Jan 2010 07:00:41 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[convertir]]></category> <category><![CDATA[página web]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[xpath]]></category> <category><![CDATA[xsl:for-each]]></category> <category><![CDATA[xsl:template]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=2164</guid> <description><![CDATA[Mediante este ejemplo vamos a iterar por los elementos de un documento XML utilizando las hojas de transformación XSLT. De esta manera conseguiremos convertir un fichero XML en una página web. En el ejemplo lo que haremos será recorrer los elementos a partir de un punto especificado mediante XPath. Lo primero que vamos a hacer [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-2164'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fconvertir-un-fichero-xml-en-una-pagina-web%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" data-count="horizontal" data-text="Convertir un fichero XML en una página web" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Mediante este ejemplo vamos a iterar por los elementos de un documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> utilizando las hojas de transformación <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a>. De esta manera conseguiremos convertir un fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> en una página web.</p><p>En el ejemplo lo que haremos será recorrer los elementos a partir de un punto especificado mediante XPath.</p><p>Lo primero que vamos a hacer es montar nuestro documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>, el cual solo contendrá la información -en este caso libros y autores- y una referencia al archivo que realizará la transformación.</p><p>Así, nuestra información será algo parecido a lo siguiente:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  ...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Y la referencia a nuestro fichero de transformación:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml-stylesheet</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/xsl&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;listarElementos.xsl&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li></ol></pre><p>Hasta aquí sencillo. Ahora veamos qué tenemos que hacer en nuestro fichero de transformación. Lo primero será definir el template sobre el que queremos trabajar. En el template hacemos referencia, usando XPath, al elemento sobre el cual nos posicionaremos inicialmente. En nuestro caso nos vamos a posicionar sobre la raíz del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Es por ello que utilizaremos la expresión XPath "/". El elemento <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> que utilizamos para definir la plantilla es <a
href="http://www.w3api.com/wiki/XSLT:Xsl:template" title="xsl:template">xsl:template</a>.</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Ahora definimos el bucle que recorrerá los elementos. Para ello utilizamos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>. Lo que estamos diciendo sería algo así como "por cada elemento que encuentres del tipo x ves iterando". Dentro de la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a> definiremos las acciones que queremos hacer con todos y cada uno de los elementos.</p><p>Para especificar el elemento sobre el que se itera, la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a> tiene el atributo <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each.select" title="select">select</a>. El cual, tendrá como parámetro un valor XPath que indica un elemento del árbol <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Así, la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a> nos quedaría de la siguiente forma:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libros/libro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>En nuestro caso trataremos cada uno de los libros. Por ello, el nodo elegido es "libros/libro".</p><p>Solo nos quedará realizar las acciones oportunas con cada uno de los libros. Este código, será el que pongamos dentro de la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>. Para nuestro ejemplo bastará con recuperar los valores del titulo y el autor. Para ello utilizamos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a> como vemos a continuación:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;autor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li></ol></pre><p>De una forma sencilla e iterando los elementos de un documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>L hemos conseguido convertir a dicho documento en una página web.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/" rel="bookmark" title="Diciembre 30, 2009">Número de elementos de un fichero XML</a></li><li><a
href="http://lineadecodigo.com/xslt/mi-primer-xslt/" rel="bookmark" title="Septiembre 4, 2009">Mi primer XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" rel="bookmark" title="Septiembre 5, 2009">Crear una tabla a partir de datos XML</a></li><li><a
href="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" rel="bookmark" title="Noviembre 6, 2009">Filtrar información en una XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/" rel="bookmark" title="Septiembre 8, 2009">Cargar un combo con XSLT</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_2164()',1000); });</script><script type="text/javascript">function loadGoogle1_2164(){ jQuery(document).ready(function($) { $('.dd-google1-2164').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Número de elementos de un fichero XML</title><link>http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/</link> <comments>http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/#comments</comments> <pubDate>Wed, 30 Dec 2009 07:00:45 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[count]]></category> <category><![CDATA[elementos]]></category> <category><![CDATA[nodo]]></category> <category><![CDATA[numero]]></category> <category><![CDATA[xpath]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=2150</guid> <description><![CDATA[Si estás tratando un fichero XML es muy probable que necesites conocer el número de elementos que lo componen, de cara a poderlo manipular de una forma más sencilla. Sobre todo si están iterando en un bucle por el número de elementos de un determinado nodo. Saber el número de elementos de un fichero XML [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-2150'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fnumero-de-elementos-de-un-fichero-xml%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/" data-count="horizontal" data-text="Número de elementos de un fichero XML" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Si estás tratando un fichero XML es muy probable que necesites conocer el número de elementos que lo componen, de cara a poderlo manipular de una forma más sencilla. Sobre todo si están iterando en un bucle por el número de elementos de un determinado nodo.</p><p>Saber el número de elementos de un fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> dado un nodo en concreto es una tarea muy sencilla.</p><p>Imaginemos el siguiente <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> (¡qué poco original que soy!):</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  ...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Lo primero que haremos en nuestro fichero <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> será el saber que parte de la estructura del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> nos interesa contar. Es decir, en un nodo en concreto, para saber el número de elementos que dicha estructura tiene. En este ejemplo, la idea es contar el número de libros que hay en el documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>.</p><p>Para contar el número de elementos disponemos de una función. Esta es la función <a
href="http://www.w3api.com/wiki/XSLT:Count()" title="count()">count()</a>:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">count(xpath)</div></li></ol></pre><p>El parámetro de la función será una expresión xpath que haga relación a alguna estructura del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> que estamos tratando. En nuestro caso la expresión xpath será:</p><pre>/libros/libro</pre><p>Para poder mostrar por pantalla el número de elementos deberemos de utilizar la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a>, la cual, en su parámetro select llevará la función <a
href="http://www.w3api.com/wiki/XSLT:Count()" title="count()">count()</a>:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;count(/libros/libro)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li></ol></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/mi-primer-xslt/" rel="bookmark" title="Septiembre 4, 2009">Mi primer XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" rel="bookmark" title="Noviembre 6, 2009">Filtrar información en una XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" rel="bookmark" title="Noviembre 28, 2009">Ordenando un fichero XML</a></li><li><a
href="http://lineadecodigo.com/php/recorrer-un-array-en-php/" rel="bookmark" title="Enero 9, 2011">Recorrer un array en PHP</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_2150()',1000); });</script><script type="text/javascript">function loadGoogle1_2150(){ jQuery(document).ready(function($) { $('.dd-google1-2150').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Ordenando un fichero XML</title><link>http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/</link> <comments>http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/#comments</comments> <pubDate>Fri, 27 Nov 2009 23:17:53 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[ordenar]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[xsl:for-each]]></category> <category><![CDATA[xsl:sort]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=2112</guid> <description><![CDATA[Cada día tratamos más fuentes de información basadas en XML. Ficheros que intercambiamos, bases de datos basadas en XML o simplemente registros que contienen información variopinta. Podemos apoyarnos en XSLT para dejar ordenado un fichero XML. Y al igual que cuando tratamos una base de datos, podemos dejarle ordenado por el campo que nosotros queramos. [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-2112'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fordenando-un-fichero-xml%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" data-count="horizontal" data-text="Ordenando un fichero XML" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Cada día tratamos más fuentes de información basadas en <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Ficheros que intercambiamos, bases de datos basadas en <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> o simplemente registros que contienen información variopinta.</p><p>Podemos apoyarnos en <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> para dejar ordenado un fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Y al igual que cuando tratamos una base de datos, podemos dejarle ordenado por el campo que nosotros queramos.</p><p>Lo primero que tenemos que hacer es definir el <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> sobre el que vamos a recuperar la información. Utilizaremos la misma estructura de libros que utilizamos en el resto de los ejemplos:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Don Juan Tenorio<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jose Zorilla<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-97-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>En primer lugar vamos a recorrer los elementos que queremos mostrar. En nuestro caso mostraremos los libros y autores de los mismos. Esto lo podemos llevar a cabo con la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>, la cual, mediante el atributo <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each.select" title="select">select</a> demarca el path del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> sobre el que queremos iterar. El path elegido será libros/libro.</p><p>En el caso de que queramos recuperar los valores de un elemento en concreto utilizamos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a>. En este etiqueta, al igual que en la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>, el atributo <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each.select" title="select">select</a> nos indicará el elemento a recuperar.</p><p>Al final nos quedará un código como el que vemos:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libros/libro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> Titulo:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> Autor:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;autor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Vemos que nuestro documento <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> combina etiquetas de transformación (etiquetas del namespace xsl) y etiquetas <a
href="http://www.manualweb.net/tutorial-xhtml/" title="XHTML">XHTML</a> que conformarán el documento definitivo, obtenido tras la transformación.</p><p>Si solo ponemos este código, nos saldrá la información, pero sin ordenar:</p><pre>Mis Libros
Titulo:Fuente Ovejuna
Autor:Lope de Vega
Titulo:La Celestina
Autor:Fernando de Rojas
Titulo:Don Juan Tenorio
Autor:Jose Zorilla
Titulo:Lazarillo de Tormes
Autor:Anonimo
Titulo:Fortunata y Jacinta
Autor:Benito Perez Galdos
Titulo:La Colmena
Autor:Camilo Jose Cela</pre><p>Para ordenar los elementos utilizamos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:sort" title="xsl:sort">xsl:sort</a>. Esta etiqueta tiene un atributo select que indica el path sobre el que queremos ordenar. Así, si queremos ordenar por nombre del titulo pondríamos la siguiente línea de código:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:sort</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li></ol></pre><p>Quedándonos el siguiente listado:</p><pre>Mis Libros
Titulo:Don Juan Tenorio
Autor:Jose Zorilla
Titulo:Fortunata y Jacinta
Autor:Benito Perez Galdos
Titulo:Fuente Ovejuna
Autor:Lope de Vega
Titulo:La Celestina
Autor:Fernando de Rojas
Titulo:La Colmena
Autor:Camilo Jose Cela
Titulo:Lazarillo de Tormes
Autor:Anonimo</pre><p>Hay que tener cuidado ya que la ordenación (<a
href="http://www.w3api.com/wiki/XSLT:Xsl:sort" title="xsl:sort">xsl:sort</a>) tiene que estar dentro de la iteración de elementos (<a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>).<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/" rel="bookmark" title="Septiembre 8, 2009">Cargar un combo con XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" rel="bookmark" title="Noviembre 6, 2009">Filtrar información en una XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" rel="bookmark" title="Septiembre 5, 2009">Crear una tabla a partir de datos XML</a></li><li><a
href="http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/" rel="bookmark" title="Diciembre 30, 2009">Número de elementos de un fichero XML</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_2112()',1000); });</script><script type="text/javascript">function loadGoogle1_2112(){ jQuery(document).ready(function($) { $('.dd-google1-2112').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Filtrar información en una XSLT</title><link>http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/</link> <comments>http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/#comments</comments> <pubDate>Fri, 06 Nov 2009 21:35:23 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[filtro]]></category> <category><![CDATA[xsl:choose]]></category> <category><![CDATA[xsl:for-each]]></category> <category><![CDATA[xsl:if]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=2103</guid> <description><![CDATA[Nuestras hojas de transformación no se tienen que limitar a recuperar toda la información de un fichero XML y a ponerla en otro tipo de formato. Puede ser que cierta información del documento no nos interese, o tenga que ser evaluada. Es por ello que puede darse el caso de que necesitemos filtrar información en [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-2103'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Ffiltrar-informacion-en-una-xslt%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" data-count="horizontal" data-text="Filtrar información en una XSLT" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Nuestras hojas de transformación no se tienen que limitar a recuperar toda la información de un fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> y a ponerla en otro tipo de formato. Puede ser que cierta información del documento no nos interese, o tenga que ser evaluada. Es por ello que puede darse el caso de que necesitemos filtrar información en una <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a>.</p><p>Para poder realizar esa evaluación de datos nos podemos apoyar en estructuras condicionales como <a
href="http://w3api.com/wiki/XSLT:Xsl:if" title="xsl:if">xsl:if</a>, <a
href="http://w3api.com/wiki/XSLT:Xsl:choose" title="xsl:choose">xsl:choose</a> y las funciones que tiene el lenguaje <a
href="http://www.manualweb.net/tutorial-xsl/" title="XSL">XSL</a>.</p><p>Siguiendo al resto de los <a
href="http://lineadecodigo.com/categoria/xslt/" title="ejemplos XSLT de nuestra web">ejemplos XSLT de nuestra web</a>, vamos a utilizar el fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> con los datos de los libros. Pero en este caso vamos a añadir un nuevo campo en la estructura. Este nuevo campo será el precio del libro. El fichero de libros nos quedará así:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;precio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>24<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/precio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;precio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>32<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/precio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  ...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>La condición de nuestra hoja de transformación será el generar un listado de libros, pero siempre y cuando, estos, tengan un precio superior o igual a 30 euros.</p><p>Para ello lo primero que tenemos que hacer es mostrar el listado de libros. Esto lo conseguimos con una estructura repetitiva del tipo <a
href="http://w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>. En medio de esta estructura, utilizando etiquetas <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of" title"=xsl:value-of">xsl:value-of</a> mostramos la información del titulo del libro, autor y precio del mismo. Tendremos el siguiente código:</p><pre class="xsl" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;xsl:for-each select=&quot;libros/libro&quot;&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  Titulo:&lt;xsl:value-of select=&quot;titulo&quot;/&gt;&lt;br/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  Autor:&lt;xsl:value-of select=&quot;autor&quot;/&gt;&lt;br/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  Precio:&lt;xsl:value-of select=&quot;precio&quot;/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;/xsl:for-each&gt;</div></li></ol></pre><p>El código anterior nos va a sacar todos los libros que tengamos definidos en nuestra estructura <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Todavía no hemos ejecutado el filtro. Para aplicar el filtro utilizamos la etiqueta xsl:if. Antes de utilizarla refrescaremos cual es su estructura</p><pre class="xsl" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;xsl:if test=&quot;condicion&quot;&gt; ... &lt;/xsl:if&gt;</div></li></ol></pre><p>Como podemos ver, el atributo test es el que nos permite evaluar la condición. Como condición podemos utilizar cualquiera de las funciones que nos da el lenguaje <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> o simplemente comparar valores de elementos del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> contra constantes. Este último caso será el nuestro. Y es que nosotros contrastaremos el valor de un elemento del documento, el precio, contra una constante, 24 (euros € o dolares $ <img
src='http://lineadecodigo.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ) Quedándonos al final esta línea de código:</p><pre class="xsl" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;xsl:if test=&quot;precio&amp;gt;24&quot;&gt; ... &lt;/xsl:if&gt;</div></li></ol></pre><p>En el medio de esta decisión pondremos la extracción de información del libro, es decir, los <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of" title"=xsl:value-of">xsl:value-of</a>. Nuevamente, quedándonos:</p><pre class="xsl" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;xsl:for-each select=&quot;libros/libro&quot;&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  &lt;xsl:if test=&quot;precio&gt;=30&quot;&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    Titulo:&lt;xsl:value-of select=&quot;titulo&quot;/&gt;&lt;br/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    Autor:&lt;xsl:value-of select=&quot;autor&quot;/&gt;&lt;br/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    Precio:&lt;xsl:value-of select=&quot;precio&quot;/&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  &lt;/xsl:if&gt;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;/xsl:for-each&gt;</div></li></ol></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" rel="bookmark" title="Noviembre 28, 2009">Ordenando un fichero XML</a></li><li><a
href="http://lineadecodigo.com/xslt/mi-primer-xslt/" rel="bookmark" title="Septiembre 4, 2009">Mi primer XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" rel="bookmark" title="Septiembre 5, 2009">Crear una tabla a partir de datos XML</a></li><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/" rel="bookmark" title="Diciembre 30, 2009">Número de elementos de un fichero XML</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_2103()',1000); });</script><script type="text/javascript">function loadGoogle1_2103(){ jQuery(document).ready(function($) { $('.dd-google1-2103').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Cargar un combo con XSLT</title><link>http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/</link> <comments>http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/#comments</comments> <pubDate>Tue, 08 Sep 2009 06:00:21 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[Combo]]></category> <category><![CDATA[hoja de transformacion]]></category> <category><![CDATA[OPTION]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[xsl:for-each]]></category> <category><![CDATA[xsl:stylesheet]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=1915</guid> <description><![CDATA[La pretensión de este ejemplo es ver como podemos cargar un combo (desplegable) mediante una hoja XSLT. Es decir, utilizar los datos de un XML para que aparezcan como opciones de un combo. Para ello partimos de nuestro XML de ejemplo de siempre. Nuestros autores. La verdad es que debería de pensar en sacarlo a [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-1915'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fcargar-un-combo-con-xslt%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/" data-count="horizontal" data-text="Cargar un combo con XSLT" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>La pretensión de este ejemplo es ver como podemos cargar un combo (desplegable) mediante una hoja <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a>. Es decir, utilizar los datos de un <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> para que aparezcan como opciones de un combo.</p><p>Para ello partimos de nuestro <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> de ejemplo de siempre. Nuestros autores. La verdad es que debería de pensar en sacarlo a una página aparte, ya que solo hago que hacer "copy & paste". Pero veamos como es el <a
href="http://www.manualweb.net/tutorial-xml/">XML</a> de autores:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Don Juan Tenorio<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jose Zorilla<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-97-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>En nuestro ejemplo vamos a cargar un combo con los títulos de los libros que están dentro del <a
href="http://www.manualweb.net/tutorial-xml/">XML</a>. A si que pongámonos manos a la obra con nuestra hoja de transformación.</p><p>Hay que recordar que las hojas de transformación son documentos <a
href="http://www.manualweb.net/tutorial-xml/">XML</a>. A si que lo primero que veremos en el será la cabecera que lo identifique <a
href="http://www.manualweb.net/tutorial-xml/">XML</a> y la definición del namespace de XSL.</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">   <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li></ol></pre><p>Esto era obvio, pero por si acaso había algún despistado, había que recordarlo.</p><p>Pero vayamos al meollo. Para conseguir la carga del combo tenemos que definir una plantilla. Dicha plantilla lo que tendrá que hacer es recorrer los elementos libro e ir generando las etiquetas <a
href="http://www.w3api.com/wiki/HTML:OPTION" title="OPTION"><OPTION></a> que son las que representan el contenido del combo. Para iterar sobre los elementos utilizamos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>.</p><p>En dicha etiqueta tenemos que establecer como atributo de iteración, <a
href="http://www.w3api.com/wiki/XSLT:Xsl:for-each.select" title="select">select</a>, el conjunto de etiquetas sobre las que queremos iterar. En nuestro caso libros/libro. Y en cada iteración obtener el valor del atributo. Para esto utilizaremos la etiqueta <a
href="http://www.w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a>, que al igual que la anterior tiene un atributo <a
href="http://www.w3api.com/wiki/XSLT:Xsl:value-of.select" title="select">select</a>, que en este caso indica la etiqueta de la cual se quiere recuperar el valor.</p><p>Al final nos queda algo así:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libros/libro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Ya que al final, lo que vamos es buscando que la hoja de transformación suelte un código <a
href="http://www.manualweb.net/tutorial-html/" title="HTML">HTML</a> como el siguiente:</p><pre class="html4strict" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SELECT"><span style="color: #000000; font-weight: bold;">select</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>Fuente Ovejuna<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>La Celestina<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>Don Juan Tenorio<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SELECT"><span style="color: #000000; font-weight: bold;">select</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre><p>A si que como has podido comprobar, dentro de la hoja de transformación entre el código anterior tendremos que añadir la etiqueta <a
href="http://www.w3api.com/wiki/HTML:SELECT" title="select">SELECT</a>. Quedándonos la hoja de transformación como sigue:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libros/libro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre><p>Y poco más. Solo recordar que el documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> tiene que tener una referencia a la hoja de transformación que lo va a usar:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml-stylesheet</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/xsl&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;combo.xsl&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li></ol></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" rel="bookmark" title="Noviembre 28, 2009">Ordenando un fichero XML</a></li><li><a
href="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" rel="bookmark" title="Septiembre 5, 2009">Crear una tabla a partir de datos XML</a></li><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" rel="bookmark" title="Noviembre 6, 2009">Filtrar información en una XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/mi-primer-xslt/" rel="bookmark" title="Septiembre 4, 2009">Mi primer XSLT</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_1915()',1000); });</script><script type="text/javascript">function loadGoogle1_1915(){ jQuery(document).ready(function($) { $('.dd-google1-1915').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Crear una tabla a partir de datos XML</title><link>http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/</link> <comments>http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/#comments</comments> <pubDate>Sat, 05 Sep 2009 07:00:47 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[table]]></category> <category><![CDATA[td]]></category> <category><![CDATA[tr]]></category> <category><![CDATA[XML]]></category> <category><![CDATA[xsl]]></category> <category><![CDATA[xsl:for-each]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=1888</guid> <description><![CDATA[Crear una tabla a partir de un documento XML que representa una lista de libros. Para ello utilizaremos las hojas de transformación XSLT.]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-1888'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fcrear-una-tabla-a-partir-de-datos-xml%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" data-count="horizontal" data-text="Crear una tabla a partir de datos XML" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>A la hora de presentar datos una de los artificios más usados son las tablas. Con el lenguaje <a
href="http://www.manualweb.net/tutorial-html/" title="HTML">HTML</a> es muy sencillo el montar una de ellas. Nos basta con controlar las etiquetas <a
href="http://w3api.com/wiki/HTML:TABLE" title="TABLE">TABLE</a>, <a
href="http://w3api.com/wiki/HTML:TR" title="TR">TR</a> y <a
href="http://w3api.com/wiki/HTML:TD" title="TD">TD</a>.</p><p>En el siguiente ejemplo utilizaremos <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> para montar una tabla <a
href="http://www.manualweb.net/tutorial-xhtml/" title="XHTML">XHTML</a>. Los datos de dicha tabla serán los datos que vayan en el <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Para ello, lo primero que haremos será crear nuestro documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> con la información a mostrar. Usamos un <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> que representa información de libros:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  ...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Básicamente, como descripción de nuestro ejemplo, lo que vamos a hacer es recorrer los elementos de un path determinado e ir creando filas de la tabla.</p><p>Pero vamos por partes. Lo primero que tenemos que hacer en nuestro documento XSL, dentro de la plantilla principal es declarar la cabecera de la tabla:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mis Libros<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">border</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr</span> <span style="color: #000066;">bgcolor</span>=<span style="color: #ff0000;">&quot;skyblue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Titulo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Autor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre><p>Una vez que tenemos la cabecera, tenemos que iterar por los elementos. Para cada fila hay que crear una etiqueta <a
href="http://w3api.com/wiki/HTML:TR" title="TR">TR</a>, que es una fila, y para cada elemento una etiqueta <a
href="http://w3api.com/wiki/HTML:TD" title="TD">TD</a>, que es una celda. Quedándonos el siguiente código:</p><pre class="xml" style="font-family:monospace;"><ol start="11"><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libros/libro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;autor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Podemos comprobar que estamos mezclando el código <a
href="http://www.manualweb.net/tutorial-xhtml/" title="XHTML">XHTML</a> con el código <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> sin ningún problema. Para recorrer los elementos del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> utilizamos la etiqueta <a
href="http://w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>, la cual, mediante el atributo <a
href="http://w3api.com/wiki/XSLT:Xsl:for-each.select" title="select">select</a> identifica un path en el árbol <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. En nuestro ejemplo, al mostrar los contenidos de los libros, el path será libros/libro.</p><p>Los valores de los elementos son recuperados mediante el <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a>, que al igual que sucede con <a
href="http://w3api.com/wiki/XSLT:Xsl:for-each" title="xsl:for-each">xsl:for-each</a>, tiene un atributo <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of.select" title="select">select</a> con el elemento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> a recuperar.</p><p>Solo nos quedará el cerrar la tabla dentro de la plantilla. El código más sencillo de todos:</p><pre class="xml" style="font-family:monospace;"><ol start="17"><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Aunque sea algo obvio, cabe recordar, que en el documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> hay que crear un enlace al documento de transformación <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a>.</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml-stylesheet</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/xsl&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;tabla.xsl&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li></ol></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" rel="bookmark" title="Noviembre 28, 2009">Ordenando un fichero XML</a></li><li><a
href="http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/" rel="bookmark" title="Septiembre 8, 2009">Cargar un combo con XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/mi-primer-xslt/" rel="bookmark" title="Septiembre 4, 2009">Mi primer XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/numero-de-elementos-de-un-fichero-xml/" rel="bookmark" title="Diciembre 30, 2009">Número de elementos de un fichero XML</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_1888()',1000); });</script><script type="text/javascript">function loadGoogle1_1888(){ jQuery(document).ready(function($) { $('.dd-google1-1888').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Mi primer XSLT</title><link>http://lineadecodigo.com/xslt/mi-primer-xslt/</link> <comments>http://lineadecodigo.com/xslt/mi-primer-xslt/#comments</comments> <pubDate>Fri, 04 Sep 2009 07:00:31 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[XSLT]]></category> <category><![CDATA[late]]></category> <category><![CDATA[match]]></category> <category><![CDATA[namespace]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[xsl:stylesheet]]></category> <category><![CDATA[xsl:value-of]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=1762</guid> <description><![CDATA[XSLT es un lenguaje que nos permite transformar documentos XML de un formato a otro. Por ejemplo transformar documentos XML en documentos XHTML u otros documentos XML. Para crear nuestro primer XSLT lo primero que tenemos que hacer es escribir nuestro fichero XML. En el caso de este ejemplo vamos a construir un documento de [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-1762'></div><g:plusone size='medium' href='http://lineadecodigo.com/xslt/mi-primer-xslt/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fxslt%2Fmi-primer-xslt%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/xslt/mi-primer-xslt/" data-count="horizontal" data-text="Mi primer XSLT" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p><a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> es un lenguaje que nos permite transformar documentos <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> de un formato a otro. Por ejemplo transformar documentos <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> en documentos <a
href="http://www.manualweb.net/tutorial-xhtml/" title="XHTML">XHTML</a> u otros documentos <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>.</p><p>Para crear nuestro primer <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> lo primero que tenemos que hacer es escribir nuestro fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. En el caso de este ejemplo vamos a construir un documento de libros como vemos a continuación:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuente Ovejuna<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lope de Vega<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-9815-002-7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Celestina<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/titulo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fernando de Rojas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/autor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>84-96390-96-9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/isbn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  ...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/libros<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></li></ol></pre><p>Lo que vamos a hacer mediante el <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> es obtener información del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> y crear un documento <a
href="http://www.manualweb.net/tutorial-xhtml/" title="XHTML">XHTML</a> donde se vean dichos datos.</p><p>Para construir el documento <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> lo primero que tenemos que saber es que, este, es a su vez otro documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Por lo tanto, la primera línea que nos encontraremos será la definición del <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>.</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li></ol></pre><p>Lo siguiente es empezar a definir la hoja de estilos y el namespace asociado</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li></ol></pre><p>En todo documento <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a>, su cuerpo, es formado por las plantillas. Estas nos indicaran a partir de que elemento del documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> vamos a empezar a trabajar. Para nuestro ejemplo, y ya que vamos a pintar en pantalla el primer libro y el primer autor, nos posicionaremos dentro de la estructura libros dela siguiente forma:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;libros&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li></ol></pre><p>El propio documento <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> combina sentencias de transformación con código <a
href="http://www.manualweb.net/tutorial-html/" title="HTML">HTML</a>. Por lo que encontraremos a continuación es un documento <a
href="http://www.manualweb.net/tutorial-html/" title="HTML">HTML</a>.</p><pre class="html4strict" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:HTML"><span style="color: #000000; font-weight: bold;">HTML</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:BODY"><span style="color: #000000; font-weight: bold;">BODY</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:H1"><span style="color: #000000; font-weight: bold;">H1</span></a>&gt;</span> Mi primer documento XSLT <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:H1"><span style="color: #000000; font-weight: bold;">H1</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">...Código XSLT...</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:BODY"><span style="color: #000000; font-weight: bold;">BODY</span></a>&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:HTML"><span style="color: #000000; font-weight: bold;">HTML</span></a>&gt;</span></div></li></ol></pre><p>Aunque las sentencias del <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> son bastantes, la que nosotros necesitamos conocer en este momento es la que nos ayuda a obtener el valor de una etiqueta <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>. Cabe indicar que todas las etiquetas <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> empiezas por xsl que es el namespace definido anteriormente.</p><p>Así, para recuperar el valor de una etiqueta usamos <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a>. Como atributo de esta etiqueta encontramos <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of.select" title="select">select</a>. El cual, nos sirve para seleccionar el nodo (elemento) del cual queremos extraer su valor:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libro/titulo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;libro/autor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></li></ol></pre><p>Tanto en el <a
href="http://w3api.com/wiki/XSLT:Xsl:template" title="template">template</a> con el atributo <a
href="http://w3api.com/wiki/XSLT:Xsl:template.match" title="match">match</a>, como en la etiqueta <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of" title="xsl:value-of">xsl:value-of</a> con el atributo <a
href="http://w3api.com/wiki/XSLT:Xsl:value-of.select" title="select">select</a> estamos utilizando valores XPath. Estos lo que vienen a reflejar, en grandes rasgos, son elementos dentro del árbol <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>.</p><p>Lo último que nos quedará hacer es reflejar en el documento <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a> la relación con su fichero de transformación. Para ello utilizamos la siguiente línea dentro del fichero <a
href="http://www.manualweb.net/tutorial-xml/" title="XML">XML</a>:</p><pre class="xml" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml-stylesheet</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/xsl&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;MiPrimerXSLT.xsl&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></div></li></ol></pre><p>Aunque la potencia del <a
href="http://www.manualweb.net/tutorial-xslt/" title="XSLT">XSLT</a> es muy grande, nos vamos a encontrar que hay navegadores que no lo soportan. En el grupo de los navegadores que si lo soportan encontramos a FireFox, Internet Explorer 6,... y en el grupo de los que no Opera 8, Internet Explorer 5,...</p><p>Seguro que en el futuro, todos los navegadores incluirán el soporte de este potente lenguaje de transformación.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/xslt/convertir-un-fichero-xml-en-una-pagina-web/" rel="bookmark" title="Enero 1, 2010">Convertir un fichero XML en una página web</a></li><li><a
href="http://lineadecodigo.com/xslt/cargar-un-combo-con-xslt/" rel="bookmark" title="Septiembre 8, 2009">Cargar un combo con XSLT</a></li><li><a
href="http://lineadecodigo.com/xslt/crear-una-tabla-a-partir-de-datos-xml/" rel="bookmark" title="Septiembre 5, 2009">Crear una tabla a partir de datos XML</a></li><li><a
href="http://lineadecodigo.com/xslt/ordenando-un-fichero-xml/" rel="bookmark" title="Noviembre 28, 2009">Ordenando un fichero XML</a></li><li><a
href="http://lineadecodigo.com/xslt/filtrar-informacion-en-una-xslt/" rel="bookmark" title="Noviembre 6, 2009">Filtrar información en una XSLT</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_1762()',1000); });</script><script type="text/javascript">function loadGoogle1_1762(){ jQuery(document).ready(function($) { $('.dd-google1-1762').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/xslt/mi-primer-xslt/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
