Is this XSLT ineffective?

I have 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 last two choices are suboptimal. Can this be done better or more efficiently?

Style sheet:

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

Source:

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

And the translation file ( 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>

Result:

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

Example: when I switch from 'en' to 'de' and I am in the first attribute name('table') in the source file, I look at the translation file for en entry, where en = "table" and select the attribute 'de' for name.

+4
source share
1 answer

:

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

+4

All Articles