Is it possible to use the sprintf () function in XSLT?

I would like to do something like "Hello% s" and have "World" in another variable.

Of course, I could make this simple case using a string replacement, but if possible, I would like all sprintf () functions, such as reordering the arguments, to be very difficult for myself.

+3
source share
2 answers

The XML / XSLT Hello World symbol is :

We have this XML document:

<t>Hello <rep/>!</t> 

and use this conversion on it :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="rep"> <xsl:value-of select="'World'"/> </xsl:template> </xsl:stylesheet> 

The result obtained is :

 <t>Hello World!</t> 

Please note :

  • We use and redefine the rule of identification . This is the most fundamental XSLT design pattern.

  • We can cross the permanent content from a variety of different replacement elements and thus have a method to "fill the gaps".

  • Using this technique, you can achieve a complete separation between content and processing logic . XSLT code does not know which source XML document it is processing (the document URL can even be passed as a parameter). In the same XSLT stylesheet, you can use any filling spaces document, and any filling spaces document can be handled by different XSLT stylesheets. This separation provides maximum flexibility and maintainability.

  • If you only need text that can be created, this can also be done easily - for example, using <xsl:output method="text"/>

+2
source

If you are performing a conversion within a language that supports the XSLT extension using an extension object (for example, one of the .NET or PHP languages); then you can simply create a function for this and call it from your XSLT.

+1
source

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


All Articles