Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format("Your query {0} with {1} Placeholders", theQuery, results.Count) Equivalent in XSLT

Tags:

c#

xslt

Is there an equivalent function to string format in XSLT?

I'm working on a multi-lingual site in umbraco. I'm not aware of what languages will be needed, butbeing as they are, one language could order the words differently e.g.

English "Your query 'Duncan' matched 5 results." could translate word for word to "5 results matched 'Duncan' query".

For this reason having a item for "Your query", "matched" and "results" in my umbraco translation isn't feasible. If I was to make this a user control for C# I would have the translator to provide a dictionary item like "Your query '{0}' matched {1} results".

like image 434
Duncan Avatar asked Nov 12 '10 16:11

Duncan


1 Answers

Is there an equivalent function to string format in XSLT?

This is a close analog in XSLT:

The dictionary entry has the following format:

<t>Your query "<query/>" matched <nResults/> results</t>

The transformation (corresponding to string.format()) is very simple:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:param name="pQuery" select="'XPath and XSLT'"/>
  <xsl:param name="pNumResults" select="3"/>

 <xsl:template match="query">
  <xsl:value-of select="$pQuery"/>
 </xsl:template>

 <xsl:template match="nResults">
  <xsl:value-of select="$pNumResults"/>
 </xsl:template>
</xsl:stylesheet>

and it produces the wanted, correct result:

Your query "XPath and XSLT" matched 3 results
like image 160
Dimitre Novatchev Avatar answered Sep 25 '22 06:09

Dimitre Novatchev