使用XSLT来翻译一个XML文件 [英] Using XSLT to translate an XML file
问题描述
我想通过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>
现在我正在考虑使用XSLT样式表,其中包含
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?我正在考虑使用< xsl:key>
,但我从来没有得到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>
推荐答案
此转换 / p>
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>
应用于提供的XML文档:
<grammar>
<element name="table" />
<element name="chair" />
</grammar>
并拥有文件 C:\temp\delete \dicts.xml
as :
and having the file C:\temp\delete\dicts.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屋!