2008 4 de maio de 2008

Engadir XSL XSL dentro

Se XML / XSL transforma é o seu Domian, entón hai momentos nos que queremos un pedazo de código dinámico para o uso elemento de biblioteca (para ser re-utilizable). O que quero dicir, probablemente podería ser máis claro con este escenario de exemplo.

Imaxina que está creando un sitio web (e usando XML, XSL transfroms por suposto) ea maioría das páxinas tería un módulo de comentarios. Ben! a continuación, copiar ou pegar este código en cada modelo de páxina (que eu non teño dicir, pero facer o mantemento e retrabalho un pesadelo) ou aínda mellor, se crea un ficheiro de inclusión que pode ser levado, onde queira que o quere na súa páxina ( s) ...
Entón, como imos crear un ficheiro XSL include e inclui-lo dentro doutro ficheiro XSL? Aquí está como ...

Só para facer as cousas claras ... Aquí está a lista rápida de ficheiros que crear ... aquí, estaremos incluíndo información sobre froitas e verduras nunha páxina pai chamado alimentos.

1. food.xml - ficheiro de datos XML de que a transformación aplicarase
2. food.xsl - arquivo XSL principal, que ha transformar o noso food.xml
3. inc_fruits.xsl - XSL incluír o ficheiro que irá procesar datos froitas
4. inc_vegtables.xsl - XSL incluír o ficheiro que irá procesar datos vetetables

Eu non creo que teño explicar moito, os códigos para elementos anteriormente mencionados, será auto-explicativo ...

FOOD.XML


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<date>July 2008</date>
<description>All about things we eat everyday</description>
<fruits type="tropical">
<item name="mango" moreinfo="http://www.mango.com">Mango is the king of fruits</item>
<item name="banana" moreinfo="http://www.banana.com">Banana once a day , keeps the doctor away</item>
<item name="orange" moreinfo="http://www.orange.com">Orange is the color and rich in vitamin C</item>
<item name="Papaya" moreinfo="http://www.papaya.com">Papaya - Hot when raw, cold when ripe</item>
</fruits>

<vegetables>
<item name="spinach" moreinfo="http://www.spinach.com">Spinach is full of iron</item>
<item name="asparagus" moreinfo="http://www.asparagus.com">Asparagus contains loads of vitamin D </item>
<item name="fenugreek" moreinfo="http://www.fenugreek.com">Fenugreek is rich in vitamin C</item>
</vegetables>
</food>


FOOD.XSL


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="inc_fruits.xsl" />
<xsl:include href="inc_vegetables.xsl" />
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<h3><xsl:value-of select="/food/description" /></h3>
Modification Date : <xsl:value-of select="/food/date" />
<hr/>
<h5> About Fruits</h5>
<xsl:call-template name="about_fruits"/>

<hr/>
<h5> About Vegetables</h5>
<xsl:call-template name="about_vegetables"/>

<hr/>
</ Body>
</ Html>
</ XSL: template>
</ XSL: stylesheet>


INC_FRUITS.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_fruits">
<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


INC_VEGETABLES.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_vegetables">
<xsl:for-each select="/food/vegetables/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Descarga de todos os ficheiros anteriores aquí (255 descargas)


NDK casa | Expresando IT | Palate expresando | Penmenship expresando | AWE expresando | expresar o meu