使用xsl创建本地化内容第3部分

时间:2012-08-07 20:50:37

标签: xml xslt localization

继续这个问题:Using xsl to create localized content part 2。我的要求现在已经改变,所以我还需要匹配属性以及文本节点的部分匹配。所以从前面的例子中可以看出:

本文件:

<html>
    <div>Lot's of html</div>
    <div>[property.to.match]</div>
    <div>
        <h1>[other.property.to.match]</h1>
        <p>
            <img src="[localized_x.jpg]" />[another.property.to.match]
        </p>
        <input type="checkbox"/>[a.label.to.match]         
        <p>Some text that shouldn't be translated followed
        by [some.text.to.translate]
        XXX [other.property.to.match]
        </p>
    </div>
</html>

应该成为:

<html>
   <div>Lot's of html</div>
   <div>The British translation</div>
   <div>
      <h1>The other language localized, but non locale based generic translation</h1>
      <p><img src="british_x.jpg">Second generic property
      </p><input type="checkbox">Generic Checkbox label
      <p>Some text that shouldn't be translated followed
                by This text follows a static bit of text
                XXX The other language localized, but non locale based generic translation

      </p>
   </div>
</html>

注意img src属性与属性匹配并被重写。是否可以更新xslt以匹配和重写这些属性?

由于 罗宾

1 个答案:

答案 0 :(得分:1)

  

是否可以更新xslt以匹配并重写这些   属性?

是。只需添加此模板

     <xsl:template match="@*[contains(., '[') and contains(., ']')]">
       <xsl:attribute name="{name()}">
         <xsl:call-template name="replaceWithLookup"/>
       </xsl:attribute>
     </xsl:template>

完整的转化现在变为:

  <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="@*[contains(., '[') and contains(., ']')]">
       <xsl:attribute name="{name()}">
         <xsl:call-template name="replaceWithLookup"/>
       </xsl:attribute>
     </xsl:template>

     <xsl:template name="replaceWithLookup" match=
      "text()[contains(., '[') and contains(., ']')]">
      <xsl:param name="pText" select="."/>

      <xsl:if test="string-length($pText)">
          <xsl:value-of select=
           "substring-before(concat($pText, '['), '[')"/>

          <xsl:variable name="vToken" select=
           "substring-before(substring-after($pText, '['), ']')"/>

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

        <xsl:choose>
         <xsl:when test="$vReplacement">
          <xsl:value-of select="$vReplacement"/>
         </xsl:when>
         <xsl:when test="$vToken">
          <xsl:value-of select="concat('[', $vToken, ']')"/>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select=
            "substring-after
              ($pText,
               substring-before(concat($pText, '['), '[')
               )"/>
         </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="$vToken">
         <xsl:call-template name="replaceWithLookup">
           <xsl:with-param name="pText" select=
            "substring-after($pText, concat('[', $vToken, ']'))"/>
         </xsl:call-template>
        </xsl:if>
      </xsl:if>
     </xsl:template>
  </xsl:stylesheet>

如果lookup2.xml文件是

<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>
    <text name="another.property.to.match">
        <en>Second generic property</en>
    </text>
    <text name="a.label.to.match">
        <en>Generic Checkbox label </en>
    </text>
    <text name="some.text.to.translate">
        <en>This text follows a static bit of text</en>
    </text>
    <text name="localized_x.jpg">
        <en_GB>british_x.jpg</en_GB>
        <en>Generic JPG</en>
    </text>
</resources>

然后,在提供的XML文档上应用转换时:

<html>
        <div>Lot's of html</div>
        <div>[property.to.match]</div>
        <div>
            <h1>[other.property.to.match]</h1>
            <p>
                <img src="[localized_x.jpg]" />[another.property.to.match]
            </p>
            <input type="checkbox"/>[a.label.to.match]
            <p>Some text that shouldn't be translated followed
            by [some.text.to.translate]
            XXX [other.property.to.match]
            </p>
        </div>
</html>

产生了想要的正确结果

<html>
   <div>Lot's of html</div>
   <div>The British translation</div>
   <div>
      <h1>The other language localized, but non locale based generic translation</h1>
      <p><img src="british_x.jpg">Second generic property

      </p><input type="checkbox">Generic Checkbox label 

      <p>Some text that shouldn't be translated followed
         by This text follows a static bit of text
         XXX The other language localized, but non locale based generic translation

      </p>
   </div>
</html>