2008 13 agost 2008

Encadenament cadena delimitada dins d'un XSL

Si el seu requeriment és per dividir un valor de node d'un XML, que conté una cadena delimitada del valor, en termes individuals, llavors has arribat al lloc correcte ... fer una ullada a l'exemple. Si vostè està familiaritzat amb una mica de XML i XSL ... No crec que vostè hauria de donar cap explicació.
A més, aquest exemple inclou l'ús de funcions com el XSL xsl: call-template, xsl: substring-before, xsl: substring-after, si això és el que està després.

XML per ser transformat (food.xml): -
Suposem que la tasca és per escurçar la cadena delimitada per comes, en els de l'etiqueta "paraules clau"

<?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>
<keywords>Fruits, Vegetables, Pulses, Meat, Cereals </keywords>
</food>

XSL (food.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="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>XSL 1.0 Delimited String Tokeniser</title>
</head>
<body>
<xsl:value-of select="food/meta"/>
<div >
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="food/keywords" />
<xsl:with-param name="delimitr" select="','" />
</xsl:call-template>

</div>
</body>
</html>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="string" />
<xsl:param name="delimitr" />
<xsl:choose>
<xsl:when test="contains($string, $delimitr)">
<div style="border:1px solid red;">
<h3><xsl:value-of select="substring-before($string,$delimitr)" /></h3>
<xsl:variable name="data" select="substring-before($string,$delimitr)"/>
</div>
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="substring-after($string, $delimitr)" /><xsl:with-param name="delimitr" select="$delimitr" /></xsl:call-template>
</xsl:when>
<xsl:otherwise>
<div style="border:1px solid red;">
<h3><xsl:value-of select="$string" /></h3>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Resultant de sortida HTML: -

<div>
<div style="border: 1px solid red;">
<h3>Fruits</h3>
</div>
<div style="border: 1px solid red;">
<h3> Vegetables</h3>
</div>
<div style="border: 1px solid red;">
<h3> Pulses</h3>
</div>
<div style="border: 1px solid red;">
<h3> Meat</h3>
</div>
<div style="border: 1px solid red;">
<h3> Cereals </h3>
</div>
</div>

No cal dir ... només canviar el paràmetre "delimitr" per al delimitador de la seva elecció


2008 4 agost 2008

Un problema amb la posició () en XSL ... o no?

Vaig tenir aquest problema en crear un Strage XSL avui. Va buscar respostes en tota la xarxa, però no podia trobar cap .... En realitat, suposo! Jo sé quina és la meva força problema era, llavors, què he de buscar?

De tota manera! Només en cas de tenir aquest mateix problema també i d'alguna manera pel poder de la força que va aconseguir aterrar en aquesta pàgina ... que m'ho agrairia tones ... I si alguna vegada ha accidentalment va aterrar en aquesta pàgina per alguna estranya sort i passar a ser un desenvolupador XSL, NO FACI UNA NOTA D'AQUESTA ARA, PER AL FUTUR causa, Si algun dia, s'enfronta a aquest problema, llavors la força no podria estar amb vostès.

Enunciat del problema:
Tinc aquest XML, on hi ha dos nodes amb diversos elements en ell i els elements entre aquests dos nodes tenen una correspondència un a un. En el següent exemple, cada element en el node <array name="PLAYURL" /> es refereix (posició corresponent) a un element en el node de <array name="SITENAME" />

<?xml version="1.0" encoding="utf-8"?>
<myplaylists>
<playlist>
<title>Best of Rest </title>
<array name="SITENAME">
<str>www.musicindiaonline.com</str>
<str>www.dhingana.com</str>
<str>www.raaga.com</str>
<str>www.smashits.com</str>
<str>www.desimusic.com</str>
<str>www.musicplug.in</str>
</array>
<array name="PLAYURL">
<str>http://www.musicindiaonline.com/123/</str>
<str>http://www.dhingana.com/play/123</str>
<str>http://www.raaga.com/123</str>
<str>http://ww.smashits.com/123</str>
<str>http://www.desimusic.com/123</str>
<str>http://www.musicplug.in/123</str>
</array>
</playlist>
</myplaylists>

Ara, tinc un bucle, en el qual recórrer <array name="SITENAME">, i vull aconseguir l'article relacionat (en la posició corresponent) en <array name="PLAYURL">,

El que normaly tractaria de fer és ...

<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
play from : <a href="{../../array[@name='PLAY']/str[position()]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>

EM SAP GREU! Això funcionarà ... Ah!! Gratament ...

Ara és probable que li agradaria provar, alguna cosa com això ...

<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos"> <xsl:value-of select="position()"/></xsl:variable>
play from : <a href="{../../array[@name='PLAY']/str[$pos]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>

I De nou, això no funcionarà ... Ara que rascar el cap, tractant de posar-se en contacte amb tots els que vostè pensa que sap alguna cosa de XSL i podria ser d'alguna ajuda ... intentar totes les opcions d'altres ... i encara funciona costum ... Bé! Això és el que em va passar el atleast ...

EXCLOURE LA DESESPERACIÓ! LA PROVA

<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos-int" select="position()" />
play from : <a href="{../../array[@name='PLAY']/str[$
<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos-int" select="position()" />
play from : <a href="{../../array[@name='PLAY']/str[$ pos-int ]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>
]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>

DONT ASK ME per què funciona. Perquè no tinc una raó ben ... si ho fas! Si us plau deixa un comentari. Pot ser molt útil. També! si vostè té un millor títol per aquest post, el mándenme una línia de :)


2008 4 maig 2008

Incloure dins de XSL XSL

Si XML / XSL transforma és el seu domian, llavors hi ha moments en que volen un tros de codi dinàmic per a ser utilitzat element de la biblioteca (que es va fer re-utilitzables). El que vull dir, probablement podria ser més clar amb aquest escenari d'exemple.

Imagini que vostè està creant un lloc web (i l'ús d'XML, XSL transfroms és clar) i la majoria de les pàgines que tenen un mòdul de comentaris. Bé! llavors o bé copiar o enganxar aquest codi en totes les plantilles de pàgina (que jo no tenen veu, però que el manteniment i la modificació d'un malson) o millor encara, crear un arxiu d'inclusió que pot ser tirat en allà on vostè ho desitja a la seva pàgina ( s) ...
Així que com podem crear un arxiu XSL INCLUDE i incloure'l dins d'un altre arxiu XSL? Aquí és com ...

Només per deixar les coses clares ... aquí està la llista ràpida dels arxius que vostè cregui ... aquí, estarem incloent informació sobre les fruites i verdures en una pàgina principal anomenada dels aliments.

1. food.xml - arxiu de dades XML en el qual s'aplica la transformació
2. food.xsl - principal arxiu XSL que transformarà la nostra food.xml
3. inc_fruits.xsl - XSL arxiu d'inclusió que es processen les dades de les fruites
4. inc_vegtables.xsl - XSL arxiu d'inclusió que es processen les dades vetetables

No crec que he d'explicar molt, els codis dels elements anteriors, s'explica per si mateix ...

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>

Descarregar tots els arxius anteriors aquí (249 descàrregues)


2008 4 abril 2008

Treballar amb atributs de node XML en XSLT

Si utilitza XML i XSL, a continuació, que podria haver arribat a través d'un temps, quan s'ha de jugar una mica amb els atributs i valors dels nodes XML que XSL. Són un munt de llocs amb informació sobre aquest llarg alè, però no vaig trobar van ser breus i precisos ... Això no és una TUTORIAL XML / XSL, però el meu intent de posar en conjunt-una mena de llista de trucs ...

El XML d'exemple que treballarem amb aquest aspecte ...

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<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>
</food>

Així que començarem a transformar la nostra per sobre de XML amb XSL

Exemple 1: Visualització de valor en un atribut seleccionat

<xsl:value-of select="/food/fruits/item[@name='orange']" /><br />
to get more information about <a href="{/food/fruits/item[@name='orange']/@moreinfo}" target="new" ><xsl:value-of select="/food/fruits/item[@name='orange']/@name" /> </a>

Resultat HTML es veurà així

Orange is the color and rich in vitamin C,
to get more information about <a href="http://www.orange.com" target="new">


Exemple 2: a través de bucle i la visualització de tots els noms d'atributs XML i els seus valors

<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>,
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>

Resultat HTML es veurà així


attribute name : name, attribute value : mango
attribute name : moreinfo, attribute value : http://www.mango.com
attribute name : name, attribute value : banana
attribute name : moreinfo, attribute value : http://www.banana.com
attribute name : name, attribute value : orange
attribute name : moreinfo, attribute value : http://www.orange.com
attribute name : name, attribute value : Papaya
attribute name : moreinfo, attribute value : http://www.papaya.com


Descarregar tots els arxius anteriors aquí (243 descàrregues)



Miri aquest espai, seguiré actualitzant aquest amb els nous descobriments ...


NDK a casa | Expressant IT | Boca Expressant | Penmenship Expressant | Awe Expressant | expressar-me