这是XSLT效率低下的吗? [英] Is this XSLT inefficient?
问题描述
我从我得到的关于翻译XML文件的最后一个问题得到了答案,这激发了我的兴趣.我想出了一个不同的解决方案,但我觉得最后两个选择次优.可以用更好或更有效的方式做到这一点吗?
I have got an answer from my last question about translating an XML file and it inspired me to play with it. I came up with a different solution, but I have the feeling that the two last selects are suboptimal. Can this be done in a nicer way or more efficient?
样式表:
<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="pFrom" select="'en'"/>
<xsl:param name="pTo" select="'de'"/>
<xsl:variable name="translations" select="document('translations.xml')"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@name">
<xsl:variable name="thisname" select="." />
<xsl:variable name="entry" select="$translations/translations/entry[attribute::node()[name()=$pFrom and $thisname=.]]"></xsl:variable>
<xsl:attribute name="name" select="$entry/attribute::node()[local-name() = $pTo]"></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
来源:
<?xml version="1.0" encoding="UTF-8"?>
<grammar>
<element name="table" />
<element name="chair" />
</grammar>
翻译文件(translations.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<translations>
<entry en="table" de="Tisch" fr="Table" />
<entry en="chair" de="Stuhl" fr="Chaise"/>
</translations>
结果是:
<grammar>
<element name="Tisch"/>
<element name="Stuhl"/>
</grammar>
一个例子:当我从'en'到'de'并且位于源文件中的第一个name
属性('table')时,我查看翻译文件中的en条目,其中en =表",然后选择名称的"de"属性.
An example: when I go from 'en' to 'de' and I am at the first name
attribute ('table') in the source file, I look into the translations file for en entry where en="table" and select the 'de' attribute for the name.
推荐答案
使用键:
<xsl:key name="en" match="translations/entry" use="@en"/>
<xsl:key name="de" match="translations/entry" use="@de"/>
<xsl:key name="fr" match="translations/entry" use="@fr"/>
然后
<xsl:template match="@name">
<xsl:attribute name="name" select="key($pFrom, current(), $translatations)/@*[local-name() = $pTo]"/>
</xsl:template>
(假设您使用的是XSLT 2.0).
(I assume you use XSLT 2.0).
这篇关于这是XSLT效率低下的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!