Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

dictionary - Using XSLT to translate an XML file

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)

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This transformation:

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

when applied on the provided XML document:

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

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>

produces the wanted, correct result:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...