使用 XSLT 翻译 XML 文件 [英] Using XSLT to translate an XML file

查看:18
本文介绍了使用 XSLT 翻译 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 XSLT 将给定​​的 XML 文件(它是一个 RelaxNG 语法)翻译成其他语言.假设 XML 文件是:

I want to translate a given XML file (it is a RelaxNG grammar) to other languages via XSLT. Suppose the XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<grammar>
  <element name="table" />
  <element name="chair" />
</grammar>

现在我想拥有一个包含像

Now I was thinking of having an XSLT stylesheet with the information like

en=table, de=Tisch, fr=table
en=chair, de=Stuhl, fr=chaise
...  (there will be many, many more entries)

但我也可以将这些信息放入一个外部文件中(我是从头开始的).你能给我建议如何制定 XSLT 吗?我想为此使用 ,但我从来没有掌握 XSLT 中的键.当我创建德语翻译时,结果应该是这样的:

But I could also put this information in to an external file (I am starting from scratch). Can you give me advice how to formulate an XSLT? I was thinking of using <xsl:key> for this but I never get the hang of keys in XSLT. The result should look like this, when I create the German translation:

<?xml version="1.0" encoding="UTF-8"?>
<grammar lang="de">
  <element name="Tisch" />
  <element name="Stuhl" />
</grammar>

推荐答案

这种转变:

<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="pFrom" select="'en'"/>
 <xsl:param name="pTo" select="'de'"/>

 <xsl:key name="kIdByLangVal" match="@dId"
  use="concat(../../@lang, '+', ../@value)"/>

 <xsl:key name="kValByLangId" match="@value"
  use="concat(../../@lang, '+', ../@dId)"/>

 <xsl:variable name="vDicts" select=
  "document('file:///c:/temp/delete/dicts.xml')"/>

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

 <xsl:template match="@name">
  <xsl:variable name="vCur" select="."/>

  <xsl:attribute name="name">
   <xsl:for-each select="$vDicts">
    <xsl:value-of select=
     "key('kValByLangId',
          concat($pTo, '+',
                key('kIdByLangVal',
                    concat($pFrom, '+', $vCur)
                   )
                 )
        )
     "/>
   </xsl:for-each>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<grammar>
    <element name="table" />
    <element name="chair" />
</grammar>

并将文件 C: empdeletedicts.xml 设为:

and having the file C: empdeletedicts.xml as:

<dictionaries>
 <dictionary lang="en">
  <word dId="1" value="table"/>
  <word dId="2" value="chair"/>
 </dictionary>
 <dictionary lang="de">
  <word dId="1" value="Tisch"/>
  <word dId="2" value="Stuhl"/>
 </dictionary>
</dictionaries>

产生想要的、正确的结果:

<grammar>
   <element name="Tisch"/>
   <element name="Stuhl"/>
</grammar>

这篇关于使用 XSLT 翻译 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆