本文介绍了使用xsl创建本地化的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出具有以下结构的输入文件:

Given an input file with the following structure:

<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>

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

And a template file that I can read into the stylesheet with the following structure:

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

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

How can I get an xsl to output a localized version of the template... So for example, if I pass en_US as a parameter to the stylesheet I would like the following output:

<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>

谢谢.

推荐答案

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

<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文档:

If the provided translation file resides at c:/temp/delete/lookup2.xml and when the transformation is applied on the provided XML document:

<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>


II. 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>

这篇关于使用xsl创建本地化的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:37