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 に答える