Una delle tecnologie che uso maggiormente nello sviluppo di progetti di mobili sono il XSL Transformations (XSLT) e mi danno la flessibilità necessaria per adattare il livello di presentazione per la compatibilità terminale del cliente (Mobile xHTML, WML, iMode ...) o l'operatore (es. Vodafone PML).
In tutti questi anni ho collezionato alcune funzioni (in realtà i modelli) XSLT Faccio la vita più facile quando si prepara alcune funzioni all'interno di un portale wap. Io li lascio nel caso qualcuno sarebbe venuto in aiuto.
Capitalizzare
Un classico. Converte una stringa di testo in modo che restituisce il primo carattere in maiuscolo e la catena resto minuscolo.
- > abcdefghijklmnopqrstuvwxyz </xsl:variable > <xsl:variable "lcletters" nome => abcdefghijklmnopqrstuvwxyz </ xsl: variable>
- > ABCDEFGHIJKLMNOPQRSTUVWXYZ </xsl:variable > Nome <xsl:variable = "ucletters"> ABCDEFGHIJKLMNOPQRSTUVWXYZ </ xsl: variable>
- > Nome <xsl:template = "capitalize">
- /> <xsl:param name = "arg" />
- /> <xsl:value-of select = "concat(translate(substring($arg,1,1), $lcletters, $ucletters), substring($arg,2))" />
- </ Xsl: template>
Esempio di utilizzo:
- > Nome <xsl:call-template = "capitalize">
- select = "/ruta/al/texto" /> <xsl:with-param name = select = "/ruta/al/texto" /> "arg"
- </ Xsl: call-template>
URL-encode
Che cosa suggerisce il suo nome. Codifica una stringa da inviare come parametro URL. Molto utile in situazioni di integrazione con i sistemi di fatturazione.
- > 0123456789ABCDEF </xsl:variable > Nome <xsl:variable = "hex"> 0123456789ABCDEF </ xsl: variable>
- select = "null" /> Nome <xsl:variable = select = "null" /> "theString"
- > Nome <xsl:template = "url-encode">
- /> <xsl:param name = "str" />
- > <xsl:if "$str" prova =>
- select = "substring($str,1,1)" /> Nome <xsl:variable = select = "substring($str,1,1)" /> "primo-char"
- <xsl:choose>
- > <xsl:when "contains($safe,$first-char)" prova =>
- /> <xsl:value-of select = "$first-char" />
- </ Xsl: when>
- <xsl:otherwise>
- > Nome <xsl:variable = "codepoint">
- <xsl:choose>
- > <xsl:when "contains($ascii,$first-char)" prova =>
- /> <xsl:value-of select = "string-length(substring-before($ascii,$first-char)) + 32" />
- </ Xsl: when>
- > <xsl:when "contains($latin1,$first-char)" prova =>
- /> <xsl:value-of select = "string-length(substring-before($latin1,$first-char)) + 160" />
- </ Xsl: when>
- <xsl:otherwise>
- > Warning: string contains a character that is out of range! <xsl:message terminare = "no"> Attenzione: contiene una stringa di caratteri che è fuori portata! Sostituendo </ xsl: message> "?".
- <xsl:text> 63 </ xsl: text>
- </ Xsl: altrimenti>
- </ Xsl: scegliere>
- </ Xsl: variable>
- select = "substring($hex,floor($codepoint div 16) + 1,1)" /> Nome <xsl:variable = select = "substring($hex,floor($codepoint div "hex-digit1" 16) + 1,1)" />
- select = "substring($hex,$codepoint mod 16 + 1,1)" /> <xsl:variable "hex-digit2" name = select = 16 + 1,1)" mod "substring($hex,$codepoint />
- /> <xsl:value-of select = "concat('%',$hex-digit1,$hex-digit2)" />
- </ Xsl: altrimenti>
- </ Xsl: scegliere>
- 1"> <xsl:if prova "string-length($str) => 1 ">
- url-encode name = "<xsl:call-template ">
- substring ( $str, 2 ) "/> name = "<xsl:with-param str "select=" sottostringa ($str, 2) "/>
- </ Xsl: call-template>
- </ Xsl: if>
- </ Xsl: if>
- </ Xsl: template>
Esempio di utilizzo:
- > Nome <xsl:call-template = "url-encode">
- select = "/ruta/al/texto" /> <xsl:with-param name = select = "/ruta/al/texto" /> "str"
- </ Xsl: call-template>
Cerca e sostituisci
Ancora una volta ciò che il suo nome suggerisce. Sostituisce un testo da un altro in una catena.
- > Nome <xsl:template = "SEARCH-AND-REPLACE">
- /> <xsl:param name = "string" />
- /> <xsl:param name = "search-for" />
- /> <xsl:param name = "replace-with" />
- <xsl:choose>
- > <xsl:when'contains($string,$search-for)' => prova
- /> <xsl:value-of select = "substring-before($string,$search-for)" />
- /> <xsl:value-of select = "$replace-with" />
- > Nome <xsl:call-template = "SEARCH-AND-REPLACE">
- select = "substring-after($string,$search-for)" /> <xsl:with-param name = select = "substring-after($string,$search-for)" /> "stringa"
- select = "$search-for" /> <xsl:with-param name = select = "$search-for" /> "ricerca-per"
- select = "$replace-with" /> <xsl:with-param name = select = "$replace-with" /> "replace-con"
- </ Xsl: call-template>
- </ Xsl: when>
- <xsl:otherwise>
- /> <xsl:value-of select = "$string" />
- </ Xsl: altrimenti>
- </ Xsl: scegliere>
- </ Xsl: template>
Esempio:
- > Nome <xsl:call-template = "url-encode">
- select = "/ruta/al/texto/original" /> <xsl:with-param name = select = "/ruta/al/texto/original" /> "stringa"
- select = "texto a buscar" /> <xsl:with-param "search-for" name = select = "texto a buscar" />
- select = "texto por el que se reemplaza" /> <xsl:with-param "replace-with" name = select = "texto posa reemplaza" />
- </ Xsl: call-template>
Se è stato utilizzato XSLT a volte avrete notato che mancano alcune funzioni "solito" e che, come tali, sono molto limitate. Ma si ha la funzionalità di base XSLT da cui si può generare praticamente qualsiasi altra cosa si possa pensare, tanto per essere chiari con quello che dici e quello che fai.
Ci tengo a precisare che queste funzioni non ho, hanno raccolto nel corso degli anni googling un po ', a volte più di un po' di vergogna non ricordo dove stavo prendendo.
Per ora questi tre sono quelli che mi sono situati. Ho un altro che uso in integrazione con i sistemi di fatturazione per convertire in Base64 , ma non riesco a trovarlo da nessuna parte.









