1

给定一个具有以下结构的输入文件:

<resources>
    <text name="property.to.match">
        <en_US>The American translation</en_US>
        <en_GB>The British translation</en_GB>
        <en>The language localized, but non locale based generic translation</en>
    </text>
    <text name="other.property.to.match">
        <en>The other language localized, but non locale based generic translation</en>
    </text
</resources>

还有一个模板文件,我可以读入具有以下结构的样式表:

<html>
    <div>Lot's of html</div>
    <div>[property.to.match]</div>
    <div>[other.property.to.match]</div>
</html>

如何让 xsl 输出模板的本地化版本...例如,如果我将 en_US 作为参数传递给样式表,我想要以下输出:

<html>
    <div>Lot's of html</div>
    <div>The American translation</div>
    <div>The other language localized, but non locale based generic translation</div>
</html>

谢谢。

4

2 回答 2

0

这是一个快速编写的样式表,应该可以完成这项工作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="lang">en_US</xsl:param>
  <xsl:variable name="text" select="document('resources.xml')//text"/>

  <xsl:template match="html">
    <html>
      <xsl:apply-templates/>
    </html>
  </xsl:template>

  <xsl:template match="div"> 
    <xsl:variable name="id" select="substring(., 2 , string-length(.)-2)"/>
    <xsl:variable name="repl">
      <xsl:choose>
        <xsl:when test="$text[@name=$id]/*[name()=$lang]">
          <xsl:value-of select="$text[@name=$id]/*[name()=$lang]"/>          
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text[@name=$id]/*[name()=substring-before($lang, '_')]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>    
    <div>
      <xsl:choose>
        <xsl:when test="substring(.,1,1)='[' and substring(.,string-length(.))=']' and $repl!=''">
          <xsl:value-of select="$repl"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>
</xsl:stylesheet>

$lang='en_US' 的输出:

<html>
  <div>Lot's of html</div>
  <div>The American translation</div>
  <div>The other language localized, but non locale based generic translation</div>
</html>

$lang='en_GB' 的输出:

<html>
  <div>Lot's of html</div>
  <div>The British translation</div>
  <div>The other language localized, but non locale based generic translation</div>
</html>
于 2012-04-26T05:43:31.983 回答
0

这是一种更有效的基于键的查找

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

 <xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
 <xsl:param name="pLang" select="'en_GB'"/>

 <xsl:key name="kLookup" match="text/*"
  use="concat(../@name, '+', name())"/>

 <xsl:variable name="vDict" select="document($pLookupPath)"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "div/text()
     [starts-with(., '[')
    and substring(., string-length(),  1) = ']'
      ]">

  <xsl:variable name="vTextName" select="substring(., 2, string-length() -2)"/>

  <xsl:for-each select="$vDict">
   <xsl:value-of select=
    "(key('kLookup', concat($vTextName, '+', $pLang))
    |
      key('kLookup', concat($vTextName, '+', substring-before($pLang, '_')))
      )[1]
    "/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

如果提供的翻译文件位于c:/temp/delete/lookup2.xml并且何时对提供的 XML 文档应用转换

<html>
    <div>Lot's of html</div>
    <div>[property.to.match]</div>
    <div>[other.property.to.match]</div>
</html>

产生了想要的正确结果

<html>
   <div>Lot's of html</div>
   <div>The British translation</div>
   <div>The other language localized, but non locale based generic translation</div>
</html>

二、XSLT 2.0 解决方案:

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

 <xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
 <xsl:param name="pLang" select="'en_GB'"/>

 <xsl:key name="kLookup" match="text/*"
  use="concat(../@name, '+', name())"/>

 <xsl:variable name="vDict" select="document($pLookupPath)"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "div/text()
     [starts-with(., '[') and ends-with(., ']')]">

  <xsl:variable name="vTextName" select="substring(., 2, string-length() -2)"/>

   <xsl:sequence select=
    "(key('kLookup', concat($vTextName, '+', $pLang), $vDict)
    ,
      key('kLookup', concat($vTextName, '+', substring-before($pLang, '_')),
          $vDict)
      )[1]
    "/>
 </xsl:template>
</xsl:stylesheet>
于 2012-04-26T12:11:18.393 回答