user1345999
user1345999

Reputation: 31

XSL values detecting string in URL

I have a multi-language site with a url structure like this: www.mysite/en/home.htm or www.mysite/es/home.htm (for english and spanish versions).

In this home.htm I have a table data from an xml files. The headers of this table, <th>, are into a xsl file.

I would like to dinamically change these values depending on the language that I would detect by that /es/ in the URL.

Can anybody help me? Thanks!

Upvotes: 2

Views: 325

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUrl" select="'www.mysite/es/home.htm '"/>

 <my:headings>
  <h lang="en">
    <description>Description</description>
    <gameType>Game Type</gameType>
  </h>
  <h lang="es">
    <description>Descripción</description>
    <gameType>Tipo De Juego</gameType>
  </h>
 </my:headings>

 <xsl:variable name="vHeadings" select="document('')/*/my:headings/*"/>

 <xsl:template match="/">

  <xsl:variable name="vLang" select=
    "substring-before(substring-after($pUrl, '/'), '/')"/>

     <table>
       <thead>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/description"/></td>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/gameType"/></td>
       </thead>
     </table>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the wanted headings:

<table>
   <thead>
      <td>Descripción</td>
      <td>Tipo De Juego</td>
   </thead>
</table>

Note: In a real-world app you may want to put the language-specific data in a separate XML file (or even in files -- one per language) -- in such a case you only need to slightly change the call to the document() function in this code.

UPDATE:

The OP has indicated in a comment that the use of document() is forbidden in his environment.

Here is the same solution with a slight modification to work without using document():

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUrl" select="'www.mysite/es/home.htm '"/>

 <xsl:variable name="vrtfHeadings">
  <h lang="en">
    <description>Description</description>
    <gameType>Game Type</gameType>
  </h>
  <h lang="es">
    <description>Descripción</description>
    <gameType>Tipo De Juego</gameType>
  </h>
 </xsl:variable>

 <xsl:variable name="vHeadings" select="ext:node-set($vrtfHeadings)/*"/>

 <xsl:template match="/">

  <xsl:variable name="vLang" select=
    "substring-before(substring-after($pUrl, '/'), '/')"/>

     <table>
       <thead>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/description"/></td>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/gameType"/></td>
       </thead>
     </table>
 </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions