📜 ⬆️ ⬇️

Trivia in XSLT 1.0

Implementing the replace (), uppercase (), lowercase () functions


An example of the implementation of the replace function in XSLT 1.0.
Scan the text for a match with the target pattern, then replace the matched text with the value string and return the resulting string.

<? 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 .

As a result, instead of
Stop war! Make peace!
will Stop war! Make love! Stop war! Make love!
')
An example implementation of the uppercase function in XSLT 1.0.
We scan the text for a match with the value pattern, then replace the matched text and return the resulting string.
This implementation assumes a change in the register of the whole word .
If the text of the sought text matches the value, we check if the found is not part of the word, and if not, we replace it. The method of transfer to lower case is similar to the method of transfer to upper.

<? 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 .

As a result, all 'xml' are transformed into 'XML'.

To give more functionality (random number generation, work with dates, extended operations with strings and regular expressions, a large number of mathematical operations), you can use extensions, for example
EXSLT (Extensions to Extensible Stylesheet Language Transformations) .
For example, to generate a random number, according to the XSLT 1.0 Pocket Reference , the following code would be:

< 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 .

The result will be three numbers from 0 to 1.

UPD.
An example of the output of the minimum / maximum values ​​(surprisingly, but in the book mentioned in the comments there is no such simple example)
Let's say there is a simple 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 .

immediately go to the solution (in case of finding the maximum)
< xsl:value-of select ="Price/Number[not(following-sibling::Number > self::Number)]" />
* This source code was highlighted with Source Code Highlighter .
>

42 is the answer to EVERYTHING :)

Source: https://habr.com/ru/post/71186/


All Articles