XSLT: How to output localized data only?

Go To StackoverFlow.com

5

Given the following XML:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="form.xsl"?>
<Document>
  <Translations>
    <Translation name="Resource">Invariant Resource</Translation>
    <Translation name="Resource" lang="en">English Resource</Translation>
    <Translation name="Resource" lang="en-CA">Canadian English Resource</Translation>
    <Translation name="Resource" lang="en-GB">British English Resource</Translation>
    <Translation name="Message">Invariant Message</Translation>
    <Translation name="Message" lang="en">English Message</Translation>
    <Translation name="Message" lang="en-CA">Canadian English Message</Translation>
    <Translation name="Message" lang="en-AU">Australian English Message</Translation>
  </Translations>
</Document>

I need to select a set of Translation elements such that the set contains unique values for the "name" attribute, and the "best match" for a given locale ('en-US', 'es-MX', 'fr', etc.). When I say best match, I would like to first look for an element with the full matching locale, then look for a match based on just the first two characters, then look for an element with no lang specified.

For example, if I pass in a locale of 'en-CA' when transforming the above data, I would like to get the following two elements:

<Translation name="Resource" lang="en-CA">Canadian English Resource</Translation>
<Translation name="Message" lang="en-CA">Canadian English Message</Translation>

But if I pass in 'en-GB', I would like to get:

<Translation name="Resource" lang="en-GB">British English Resource</Translation>
<Translation name="Message" lang="en">English Message</Translation>

And finally if I pass in a value such as 'es' or 'es-MX', I would expect to get:

<Translation name="Resource">Invariant Resource</Translation>
<Translation name="Message">Invariant Message</Translation>

I'm extremely new to XSLT, but I think I have something that works. I just need to know if there is a better way to do it (simpler, more elegant, more performant, etc.)

Here's my first stab at it:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>

  <xsl:key match="Translation" name="TranslationName" use="concat(@name,':',@lang)"/>

  <xsl:template match="/">
    <!-- locale parameter for translation -->
    <xsl:param name="locale"/>

    <xsl:for-each select="Document/Translations/Translation[@lang=$locale or @lang=substring($locale,1,2) or not(@lang)]">
      <xsl:choose>
        <xsl:when test="@lang=$locale and count(key('TranslationName', concat(@name,':',$locale)))=1">
          <xsl:element name="p">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:when>
        <xsl:when test="@lang=substring($locale,1,2) and count(key('TranslationName', concat(@name,':',$locale)))=0">
          <xsl:element name="p">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:when>
        <xsl:when test="not(@lang) and count(key('TranslationName', concat(@name,':',$locale))|key('TranslationName', concat(@name,':',substring($locale,1,2))))=0">
          <xsl:element name="p">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

This is my first time to post a question, so please let me know if I need to add/edit/remove anything.

Thanks!

2012-04-05 19:32
by Nick
+1 for a good first question - Daniel Haley 2012-04-05 19:39
note, that your xsl for en-AU set on a file with only en-CA, en-GB and no en will not produce output. My answer, hopefully corrects this also. After all, they can understand each other if they really try, no - panda-34 2012-04-05 23:20
While I agree, the should be able to understand each other, my requirements are very clear about how they want the language fallback to work. But thanks for catching that - Nick 2012-04-06 20:36


0

if you could use msxsl:node-set or the like, you might do it like:

<xsl:template match="/">
    <xsl:param name="locale" select="'en-AU'"/>
<!-- locale parameter for translation -->
    <xsl:variable name="sorted">
        <xsl:for-each select="Document/Translations/Translation">
            <xsl:sort select="@name"/>
            <xsl:sort select="not(@lang=$locale)"/>
            <xsl:sort select="not(starts-with(@lang, substring($locale,1,2)))"/>
            <xsl:sort select="@lang"/>
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:variable>
    <xsl:for-each select="msxsl:node-set($sorted)/*">
        <xsl:if test="position() = 1 or @name!=preceding-sibling::*[1]/@name">
            <xsl:copy-of select="."/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

P.S. This one might work on standard 1.0

<xsl:template match="/">
    <xsl:param name="locale" select="'en-AU'"/>
<!-- locale parameter for translation -->
    <xsl:variable name="path" select="Document/Translations/Translation"/>
    <xsl:for-each select="$path">
        <xsl:variable name="curName" select="$path[@name=current()/@name]"/>
        <xsl:if test="count($curName[1] | .)=1">
            <xsl:for-each select="$curName">
            <xsl:sort select="not(@lang=$locale)"/>
            <xsl:sort select="not(starts-with(@lang, substring($locale,1,2)))"/>
            <xsl:sort select="@lang"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="."/>
            </xsl:if>
            </xsl:for-each>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

P.P.S. If you don't want to sort you might just do the filtering (preserves document order). Also, different grouping mechanism:

<xsl:template match="/">
    <xsl:param name="locale" select="'en'"/>
    <xsl:variable name="locale-lang" select="substring($locale,1,2)"/>
<!-- locale parameter for translation -->
    <xsl:variable name="path" select="Document/Translations/Translation"/>
    <xsl:for-each select="$path[not(preceding-sibling::Translation/@name=@name)]">
        <xsl:variable name="curName" select="$path[@name=current()/@name]"/>
        <xsl:variable name="test1" select="$curName[@lang=$locale]"/>
        <xsl:variable name="test2" select="$curName[@lang=$locale-lang]"/>
        <xsl:variable name="test3" select="$curName[starts-with(@lang, $locale-lang)]"/>
        <xsl:variable name="test4" select="$curName[not(@lang)]"/>
        <xsl:choose>
            <xsl:when test="$test1">
                <xsl:copy-of select="$test1[1]"/>
            </xsl:when>
            <xsl:when test="$test2">
                <xsl:copy-of select="$test2[1]"/>
            </xsl:when>
            <xsl:when test="$test3">
                <xsl:copy-of select="$test3[1]"/>
            </xsl:when>
            <xsl:when test="$test4">
                <xsl:copy-of select="$test4[1]"/>
            </xsl:when>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>
2012-04-05 22:06
by panda-34
These are all great! Right now, I think I may use a hybrid approach with pieces from each. Thanks - Nick 2012-04-06 20:37


0

This short and simple transformation (no variables, xsl:choose, xsl:when, xsl:otherwise, xsl:if, xsl:sort, xsl:element):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pCode" select="'en-GB'"/>

 <xsl:key name="kTransName" match="@name" use="."/>

 <xsl:key name="Resource" match="Translation[@name='Resource']"
          use="@lang"/>
 <xsl:key name="Message" match="Translation[@name='Message']"
          use="@lang"/>
 <xsl:key name="Resource" match="Translation[@name='Resource']"
                          use="boolean(@lang)"/>
 <xsl:key name="Message" match="Translation[@name='Message']"
                          use="boolean(@lang)"/>

 <xsl:template match="/">

  <xsl:for-each select=
   "/*/*/*/@name[generate-id()=generate-id(key('kTransName', .)[1])]">
      <xsl:copy-of select=
       "key(., $pCode)
       |
        key(., substring($pCode, 1, 2))
          [not(key(current(), $pCode))]
       |
        key(., 'false')
          [not(key(current(), $pCode)
              |
               key(current(), substring($pCode, 1, 2))
              )
          ]
       "/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Document>
    <Translations>
        <Translation name="Resource">Invariant Resource</Translation>
        <Translation name="Resource" lang="en">English Resource</Translation>
        <Translation name="Resource" lang="en-CA">Canadian English Resource</Translation>
        <Translation name="Resource" lang="en-GB">British English Resource</Translation>
        <Translation name="Message">Invariant Message</Translation>
        <Translation name="Message" lang="en">English Message</Translation>
        <Translation name="Message" lang="en-CA">Canadian English Message</Translation>
        <Translation name="Message" lang="en-AU">Australian English Message</Translation>
    </Translations>
</Document>

produces the wanted, correct result:

<Translation name="Resource" lang="en-GB">British English Resource</Translation>
<Translation name="Message" lang="en">English Message</Translation>

If we change the global/external parameter to:

 <xsl:param name="pCode" select="'en-CA'"/>

again the correct result is produced:

<Translation name="Resource" lang="en-CA">Canadian English Resource</Translation>
<Translation name="Message" lang="en-CA">Canadian English Message</Translation>

If we change the global/external parameter to:

 <xsl:param name="pCode" select="'es-MX'"/>

again the wanted result is produced:

<Translation name="Resource">Invariant Resource</Translation>
<Translation name="Message">Invariant Message</Translation>
2012-04-06 03:09
by Dimitre Novatchev
Thanks for the answer, but unfortunately there will be many more possible values for @name other than just "Resource" and "Message". I guess I should have made that a little more clear in the question - Nick 2012-04-06 20:32
Ads