Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:02:47+00:00 2026-06-01T13:02:47+00:00

Given the following XML: <?xml version=1.0 encoding=UTF-8 ?> <?xml-stylesheet type=text/xsl href=form.xsl?> <Document> <Translations> <Translation

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:02:48+00:00Added an answer on June 1, 2026 at 1:02 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in xslt <?xml version=1.0 encoding=UTF-8?> <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns:fn=http://www.w3.org/2005/02/xpath-functions
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <application name=foo> <movie name=tc english=tce.swf chinese=tcc.swf
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <process name=TestSVG2 xmlns=http://www.example.org targetNamespace=http://www.example.org xmlns:xsd=http://www.w3.org/2001/XMLSchema> <sequence>
Given the following xml file : <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE Clients SYSTEM bank.dtd> <Clients>
Given the following XML: <?xml version=1.0 encoding=UTF-8?> <userAttributeList> <attribute> <userId>12345678</userId> <attId>1234</attId> <attName>Group</attName> <attTypeId>8</attTypeId> <attTypeName>User
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <doc> <head> <title>Introduction</title> <section>section</section> <channel>testing/test</channel> </head>
Given this xml document: <?xml version=1.0 encoding=UTF-8?> <mydoc> <foo f=fooattr> <bar r=barattr1> <baz z=bazattr1>this
Given the following XML file: <?xml version=1.0 encoding=utf-8?> <Wix xmlns=http://schemas.microsoft.com/wix/2006/wi> <Fragment> <DirectoryRef Id=INSTALLLOCATIONAGENT> <Component
Given the following XML. <?xml version=1.0 encoding=UTF-8?> <xmldata> <Products> <ProductCode>M406789</ProductCode> <ProductID>858</ProductID> <ProductName>M406789 Ignition Box</ProductName>
Given this XML in data.xml <?xml version=1.0 encoding=utf-8?> <data> <bar>100</bar> </data> I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.