2

我有一个多语言网站,其 url 结构如下:www.mysite/en/home.htmwww.mysite/es/home.htm(对于英语和西班牙语版本)。

在这个 home.htm 中,我有一个来自 xml 文件的表数据。此表的标头 ,<th>位于 xsl 文件中。

我想根据我/es/在 URL 中检测到的语言动态地更改这些值。

  • 如果 URL = www.mysite/en/home.htm 那么
  • 描述
  • 游戏类型
  • ……

  • 如果 URL = www.mysite/es/home.htm 比

  • 描述
  • 游戏提示
  • ……

有谁能够帮我?谢谢!

4

1 回答 1

2

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>
于 2012-05-08T12:23:50.637 回答