Fonction translate() | |
Permet de convertir des caractères individuels en chaîne entre les différentes valeurs. Dans de nombreux langages, cette fonction est suffisamment puissante pour modifier la casse des caractères. | |
Entrées | |
Trois chaînes. Le première chaîne représente la chaîne originale non traduite, et les deuxième et troisième chaînes définissent les caractères à convertir. |
|
Sortie | |
La chaîne originale traduite comme suit :
|
|
Définie dans | |
XPath section 4.2, Fonctions Chaîne. |
|
Exemple | |
La feuille de style suivante illustre plusieurs exemples de la fonction translate() : <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:text>Tests of the translate() function:</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text>Convert a string to uppercase:</xsl:text> <xsl:value-of select="$newline"/> <xsl:text> translate('Doug', 'abcdefghijklmnopqrstuvwxyz', </xsl:text> <xsl:value-of select="$newline"/> <xsl:text> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=</xsl:text> <xsl:value-of select="translate('Doug', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text>Convert a string to lowercase:</xsl:text> <xsl:value-of select="$newline"/> <xsl:text> translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', </xsl:text> <xsl:value-of select="$newline"/> <xsl:text> 'abcdefghijklmnopqrstuvwxyz')=</xsl:text> <xsl:value-of select="translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text>Remove parentheses, spaces, and dashes</xsl:text> <xsl:text> from a U.S. phone number:</xsl:text> <xsl:value-of select="$newline"/> <xsl:text> translate('(555) 555-1212', '() -', '')=</xsl:text> <xsl:value-of select="translate('(555) 555-1212', '() -', '')"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text>Replace all but the last four digits of a </xsl:text> <xsl:text>credit card number with Xs:</xsl:text> <xsl:value-of select="$newline"/> <xsl:variable name="credit" select="'4918 3829 9920 1810'"/> <xsl:text> $credit='</xsl:text> <xsl:value-of select="$credit"/> <xsl:text>'</xsl:text> <xsl:value-of select="$newline"/> <xsl:text> translate(substring($credit, 1, 15), </xsl:text> <xsl:text>'1234567890 ', 'XXXXXXXXXX-')</xsl:text> <xsl:value-of select="$newline"/> <xsl:text> substring($credit, 16)</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> The first part is </xsl:text> <xsl:value-of select="translate(substring($credit, 1, 15), '123457890 ', 'XXXXXXXXX-')"/> <xsl:value-of select="$newline"/> <xsl:text> The second part is </xsl:text> <xsl:value-of select="substring($credit, 16)"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> Here's how they look together: </xsl:text> <xsl:value-of select="translate(substring($credit, 1, 15), '123457890 ', 'XXXXXXXXX-')"/> <xsl:value-of select="substring($credit, 16)"/> </xsl:template> </xsl:stylesheet> L'utilisation de cette feuille de style avec tout document XML renvoie les résultats suivants : Tests of the translate() function: Convert a string to uppercase: translate('Doug', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=DOUG Convert a string to lowercase: translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=doug Remove parentheses, spaces, and dashes from a U.S. phone number: translate('(555) 555-1212', '() -', '')=5555551212 Replace all but the last four digits of a credit card number with Xs: $credit='4918 3829 9920 1810' translate(substring($credit, 1, 15), '1234567890 ', 'XXXXXXXXXX-') substring($credit, 16) The first part is XXXX-XXXX-XXXX- The second part is 1810 Here's how they look together: XXXX-XXXX-XXXX-1810 |