0
votes

I am using XSL transform on XML. Some part of transform are dependent on current locale. Is there any way to find the current locale from within XSL?

For example, the user-visible serialization of a floating point number can vary between locales ("1.0" in English is "1,0" in German), and my transform needs to take differences like these into account.

2
Maybe I need more coffee to finally wake up, but, what is the culture in(!) an XSL?Patrick Cornelissen
Even if you do pass a parameter (as @Tomalak suggested), how do you plan to load culture sensitive data within the XSL file?Cerebrus
e.g. for german culture I need different number format as compared for englishRamesh Soni
Ramesh, people are voting it down because "culture in xSL" makes no sense. 1. What is "culture"? and 2. What is that in the context of "XSL"? Again, do you mean something like a country-code? And if yes (or otherwise) edit your question so that people understand what you mean. So far no one seems to fully comprehend what you're trying to do, in what environment, and how you're going about it. More info, please.AlexanderJohannesen
I think you would get more of a positive response if you used the word 'locale' - that's the conventional term for the concept you are trying to describe.Nic Gibson

2 Answers

2
votes

Pass it in as a parameter.

<xsl:param name="CultureInfo" select="'default'" />

Place an XSL param on the top level of your stylesheet, and set it from "outside" before you invoke the stylesheet.

The other (proprietary) way would be to register extension functions and ask from within the stylesheet.

2
votes

Locals aren't natively supported in XSLT 1.0, but there are ways to work with them, but you need to pass it in as a parameter to your XSLT processor or rely on extensions. Some processors will grant you access to the locale information, but there is no standard way of doing it, and we must know your environment to see if it is possible. However, in XSLT 2.0 some of the date, time and number formatting functions will have (sometimes limited) support for locales, and will be handled by the processor, not by you (which is why there's these functions :)

As to using some form of locality in a XSLT 1.0 environment and partially as a Good Thing (TM) regardless of XSLT version, I'd set up a few basic templates for the type of data you're working with, and pass the local parameter in (as explained before). For numbers, look for the xsl:format-number, for example.

As to other local things, I'd create another XML file, and let the parameter work as a picker for the info you're after. The XML might look like;

<locals>
   <config id="de">
     <currency notation="&#8352;" format-number="###.###,##" />
     <text id="welcome">Wilcommen!</text>
   </config>
   <config id="en">
     <currency notation="&#163;" format-number="### ###.##" />
     <text id="welcome">Welcome!</text>
   </config>
   <config id="no">
     <currency notation="NOK" format-number="###.###,##" />
     <text id="welcome">Velkommen!</text>
   </config>
</locals>

From this, open it as ;

<xsl:variable name="locale" select="document('locale.xml')/locals/config[@id=$parameter.for.locale]" />

And you can use it as such for the welcome text;

<xsl:value-of select="$locale/text[@id='welcome']" />

For number formatting and such it's a bit more complex, but verify this is the right path first and I'll expand, but basically you should make a few keys over the locale XML file, and use a key lookup for values. For numbers and such (need to tell me more about what you want to support) I'd write a couple of templates that deal with that, and use ;

<xsl:call-template name="my-template">
   <xsl:with-param name="this" select="'some_number'" />
</xsl:call-template>