#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    34
    Rep Power
    15

    Question Access multi-lingual xml nodes directly with XSLT


    Hi experts,

    I'm developing a system where I can output several documents in various languages. Therefore I've starte to append a <language /> Tag for the specific text.

    This looks like follows:

    Code:
    <name>
        <language id="LAN0000001-0000001">Gain orders</language>
    </name>
    As there are several language tags in one document I would like to access the language text directly, whereby I set the language once per document in a central variable.

    How could I access these tags directly. I'd like it somehow as follows:

    Code:
    <xsl:value-of select="name[$language]" />
    It would be great if you could help me.

    Regards
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    USA
    Posts
    334
    Rep Power
    15
    Here is one way of doing what you want using the xml:lang element.

    Code:
    <root>
       <numbers>
          <number desc="one">1</number>   
          <number desc="two">2</number>
       </numbers>
       <phrases> 
           <phrase key="one" xml:lang="en">The value of one is:</phrase>
           <phrase key="one" xml:lang="es">El valor de uno es:</phrase>
           <phrase key="two" xml:lang="en">The value of two is:</phrase>
           <phrase key="two" xml:lang="es">El valor de dos es:</phrase>
       </phrases>
    </root>
    Code:
    <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:param name="language" select="'en'"/>
    
      <xsl:variable name="phrases" select="/root/phrases"/>
    
      <xsl:template match="/">
          <xsl:apply-templates select="root/numbers/number"/>
      </xsl:template>
    
      <xsl:template match="number"> 
         OUTPUT: <xsl:value-of select="concat($phrases/phrase[@key=current()/@desc and lang($language)],.)"/> 
      </xsl:template>
    
    </xsl:stylesheet>
    You select the desired output language by passing a parameter to the stylesheet using whatever parameter passing mechanism is supported by your particular XSL processor. The default output langauge is set to be English i.e. en.

    Comments on this post

    • priv53 agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    34
    Rep Power
    15
    That's a great solution - thanks a lot.

IMN logo majestic logo threadwatch logo seochat tools logo