文字を置換するtranslate()関数

■(書式)translate(str1,str2,str3)

・文字列str1を、文字列str2と文字列str3の変換規則に従い置換した文字列を返す。
・文字列str2と文字列str3の変換規則・・・文字列str2の単一文字を、文字列str2と同じ配置位置にある文字列str3の単一文字に置換する。

*文字列str2が文字列str3より長く、文字列str3に置換文字が存在しない場合は削除となる。
*(例)translate(data/name,'ab\c','xy%')
   aはxに、bはyに、\は%に置換される。cは削除される。

[サンプルXML文書を表示](下のフレームに)

(例1)大英文字を小英文字に置換
(例2)記号、漢字を置換。また空白を削除
(例3)3つの引数ともノードを指定。

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="translate01.xsl"?>
<root>
<title>TRANSLATE()関数</title>
<data>
<sample>大阪 / 道頓堀</sample>
<置換元>!!!=チカン====!!!</置換元>
<置換される文字>!=チカン</置換される文字>
<置換文字>* 置換</置換文字>
</data>
</root>

XSLソース

<?xml version="1.0" encoding="Shift_Jis"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="/">
(例1)<xsl:value-of select="root/title"/>--→
<xsl:value-of select="translate(root/title,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/><br/>
(例2)<xsl:value-of select="root/data/sample"/>--→
<xsl:value-of select="translate(root/data/sample,'/道頓堀 ','・日本橋')"/><br/>
(例3)<xsl:value-of select="root/data/置換元"/>--→
<xsl:value-of select="translate(root/data/置換元,root/data/置換される文字,root/data/置換文字)"/>
</xsl:template>
</xsl:stylesheet>

end(04/8/13)