XSLT 1.0の雑学

関数の実装()、大文字()、小文字()





XSLT 1.0のreplace関数の実装例。

ターゲットパターンに一致するようにテキストをスキャンし、一致したテキストを文字列で置き換えて、結果の文字列を返します。



<? 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" indent ="yes" />



< xsl:template match ="/" >

< xsl:variable name ="string" > Stop war! Make peace! </ xsl:variable >



< xsl:call-template name ="Replace" >

< xsl:with-param name ="string" select ="$string" />

< xsl:with-param name ="target" select ="'peace'" />

< xsl:with-param name ="value" select ="'love'" />

</ xsl:call-template >

</ xsl:template >



< xsl:template name ="Replace" >

< xsl:param name ="string" />

< xsl:param name ="target" />

< xsl:param name ="value" />

< xsl:choose >

< xsl:when test ="contains( $string, $target)" >

< xsl:call-template name ="Replace" >

< xsl:with-param name ="string" select ="concat(substring-before($string, $target), $value, substring-after($string, $target))" />

< xsl:with-param name ="target" select ="$target" />

< xsl:with-param name ="value" select ="$value" />

</ xsl:call-template >

</ xsl:when >

< xsl:otherwise >

< xsl:value-of select ="$string" />

</ xsl:otherwise >

</ xsl:choose >

</ xsl:template >



</ xsl:stylesheet >


* This source code was highlighted with Source Code Highlighter .






その結果、代わりに

Stop war! Make peace!





Stop war! Make love!



Stop war! Make love!







XSLT 1.0の大文字関数の実装例。

テキストをスキャンしてパターンに一致させ、一致したテキストを置き換えて、結果の文字列を返します。

この実装では、 単語全体のレジスタを変更することを想定しています。

目的の値のテキストがテキスト内で一致する場合、見つかった部分が単語の一部であるかどうかを確認し、そうでない場合は置換します。 小文字の変換方法は、大文字の転送方法に似ています。



<? 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" indent ="yes" />



< xsl:variable name ="lower" > abcdefghijklmnopqrstuvwxyz </ xsl:variable >

< xsl:variable name ="upper" > ABCDEFGHIJKLMNOPQRSTUVWXYZ </ xsl:variable >



< xsl:template match ="/" >

< xsl:variable name ="text" > (Well-formed). xml, xml- </ xsl:variable >



< xsl:call-template name ="Uppercase" >

< xsl:with-param name ="text" select ="$text" />

< xsl:with-param name ="value" select ="'xml'" />

</ xsl:call-template >

</ xsl:template >



< xsl:template name ="Uppercase" >

< xsl:param name ="text" />

< xsl:param name ="value" />

< xsl:choose >

< xsl:when test ="contains($text, $value)" >

< xsl:variable name ="symbolBefore" select ="substring(substring-before($text, $value), string-length(substring-before($text, $value)) )" />

< xsl:variable name ="symbolAfter" select ="substring(substring-after($text, $value), 1,1)" />



< xsl:choose >

< xsl:when test ="not(contains($lower,$symbolBefore) and contains($lower,$symbolAfter))" >

< xsl:call-template name ="Uppercase" >

< xsl:with-param name ="text" select ="concat(substring-before($text, $value),translate($value,$lower,$upper),substring-after($text, $value))" />

< xsl:with-param name ="value" select ="$value" />

</ xsl:call-template >

</ xsl:when >

< xsl:otherwise >

< xsl:value-of select ="$text" />

</ xsl:otherwise >

</ xsl:choose >

</ xsl:when >

< xsl:otherwise >

< xsl:value-of select ="$text" />

</ xsl:otherwise >

</ xsl:choose >

</ xsl:template >

</ xsl:stylesheet >




* This source code was highlighted with Source Code Highlighter .






その結果、すべての「xml」が「XML」に変換されます。



より多くの機能(乱数の生成、日付の操作、文字列と正規表現を使用した高度な操作、多数の数学的な操作)を提供するには、たとえば、拡張機能を使用します

EXSLT(拡張可能なスタイルシート言語変換の拡張)

たとえば、 XSLT 1.0 Pocket Referenceに従って乱数を生成するには、次のコードがあります。



< xsl:stylesheet version ="1.0"

xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"

xmlns:random ="http://exslt.org/random"

exclude-result-prefixes ="random" >

< xsl:output indent ="yes" omit-xml-declaration ="yes" />



< xsl:template match ="/" >

< xsl:copy-of select ="random:random-sequence(3)" />

</ xsl:template >



</ xsl:stylesheet >


* This source code was highlighted with Source Code Highlighter .






結果は、0〜1の3つの数値になります。



UPD。

出力の最小値/最大値の例(驚くべきことですが、コメント言及されている本では、そのような単純な例はありません)

単純なxmlがあるとしましょう

< Price >

< Number > 15 </ Number >

< Number > 7 </ Number >

< Number > 8 </ Number >

< Number > 11 </ Number >

< Number > 3 </ Number >

< Number > 42 </ Number >

< Number > 21 </ Number >

< Number > 6 </ Number >

</ Price >

* This source code was highlighted with Source Code Highlighter .






すぐに解決策に進みます(最大値を見つける場合)

< xsl:value-of select ="Price/Number[not(following-sibling::Number > self::Number)]" />

* This source code was highlighted with Source Code Highlighter .

>







42- すべてへの答え:)



All Articles