[xsl] RE: Modifying a variable while merging 2 XMLs documents !?

Subject: [xsl] RE: Modifying a variable while merging 2 XMLs documents !?
From: Jérôme Haguet <j.haguet@xxxxxxxxxx>
Date: Fri, 17 Mar 2006 10:51:18 +0100
Hello David

Thanks for your suggestion. 
I try your solution, but I get a "Syntax Error" 
- FYI, I am using XSLT through Apache Ant 1.6.5 -

So I suppress, the first part, and add one extra /* that was missing.
I get the same result than the original one in a more concise way.
  ...
  <xsl:template match="entry">
    <xsl:copy>
      <xsl:variable name="value_fr" select="value[@xml:lang='fr']"/>
      <xsl:variable name="value_en" select="value[@xml:lang='en']"/>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:copy-of select="document('glossaryNL.xml')/*/*[value[@xml:lang='en']=$value_en][value[@xml:lang='fr']=$value_fr]/value[@xml:lang='nl']"/>
    </xsl:copy>
  </xsl:template>
  ...

So, I still have my original problem : I do not want to copy twice the translation if in the glossary the it is doubled.

Let's consider this : 
   ...
    <xsl:copy>
      <xsl:variable name="value_fr" select="value[@xml:lang='fr']"/>
      <xsl:variable name="value_en" select="value[@xml:lang='en']"/>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:for-each select="$doc2/*/* [value[@xml:lang='fr']=$value_fr] [value[@xml:lang='en']=$value_en]">
	<xsl:variable name="value_nl"    select="????self::???? value[@xml:lang='nl']"/> ######
	<xsl:if test="not($value_nl)">                                                  ######
	  <xsl:copy-of select="./value[@xml:lang='nl']"/>
	</xsl:if>
      </xsl:for-each>
    </xsl:copy>
   ...

My question : how to get in value_nl the CURRENT value of the nl translation ? 
(Because it has eventually been added previously in the for-each loop)

Thaks for your help.


JC)rC4me Haguet

-----Message d'origine-----
Subject: Re: [xsl] Modifying a variable while merging 2 XMLs documents !?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 16 Mar 2006 16:21:47 GMT

> For that, I used a variable which I change once I copy.

You can not change the value of a variable once it is bound.
    <xsl:variable name="no_value_nl" select="string-length ($value_nl) &lt; 1"/>
..... 
    <xsl:variable name="no_value_nl" select="false"/>
   </xsl:if>

This should generate an error message in xslt1, in xslt2 it's allowed
(but the second is essentially an unrelated variable that happens to
have the same name) The variable goes out of scope immediately at the
</xsl:if> as the scope of a variable in xslt is always the surrounding
element. Also select="false" selects a child <false> element but I
suspect you meant the boolean select="false()"

I think you just want something like

    <xsl:copy>
    <xsl:variable name="value_fr" select="value[@xml:lang='fr']"/>
    <xsl:variable name="value_en" select="value[@xml:lang='en']"/>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:copy-of
    select="self::*[not(value/xml:lang='nl')]/
                  document('glossaryNL.xml')/
                  *[value[@xml:lang='en']=$value_en][value[@xml:lang='fr']=$value_fr]/
                   value[@xml:lang='nl']"/>

     </xsl:copy>

David

-----Message d'origine-----
De : JC)rC4me Haguet 
EnvoyC) : jeudi 16 mars 2006 16:54
C : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : Modifying a variable while merging 2 XMLs documents !?

Hello 
 
Here is the context :
- I have one XML document which contains my software messages, translated in several languages. 
- I have one XML glosary with the translated messages for english, french and a new supported language (nl)
- I want to includes in the first document the new available translation
 
The basic criteria is : If english and french messages match, than insert the dutch translation.
The point is that in the glossary, some terms are repeated (cf. "CrC)er/Create")
So, once I have found one matching item to copy, I do not want to insert a 2nd translation.
For that, I used a variable which I change once I copy.

But it does not work ...

What is the mistake I made ? 
Is the a more consise way to do that work (no variable for value_fr and value_en) ? 


Thank you in advance 

JC)rC4me 
 
My software messages : 
<STR-Dictionary name="dic000-dial">
  <entry name="Dic_Create">
    <value xml:lang="fr">CrC)er</value>
    <value xml:lang="en">Create</value>
    <value xml:lang="es">Crear</value>
    <value xml:lang="pt">Criar</value>
    <value xml:lang="it">Crea</value>
    <value xml:lang="ja">C$B=EC&KB</value>
    <value xml:lang="de">Neu</value>
    <value xml:lang="zh-TW">C%B;B:C'B+b9</value>
    <value xml:lang="zh-CN">C%B;B:C'B+b9</value>
  </entry>
  <entry name="Dic_Rename">
    <value xml:lang="fr">Renommer</value>
    <value xml:lang="en">Rename</value>
    <value xml:lang="es">Renombrar</value>
    <value xml:lang="pt">Renomear</value>
    <value xml:lang="it">Rinomina</value>
    <value xml:lang="ja">C%BB
C%b0B
$,2>NC%B$b0C&b:B4(B</value>
    <value xml:lang="de">Umbenennen</value>
    <value xml:lang="zh-TW">C&b:B4C%BB
</value>
    <value xml:lang="zh-CN">C&b:B4C%BB
</value>
  </entry>
</STR-Dictionary>
 
The translated glossary : 
<glossary>
  <entry name='ID_2'>
    <value xml:lang='fr'>CrC)er</value>
    <value xml:lang='en'>Create</value>
    <value xml:lang='nl'>Aanmaken</value>
  </entry>
  <entry name='ID_3'>
    <value xml:lang='fr'>Supprimer</value>
    <value xml:lang='en'>Delete</value>
    <value xml:lang='nl'>Verwijderen</value>
  </entry>
  <entry name='ID_6'>
    <value xml:lang='fr'>Renommer</value>
    <value xml:lang='en'>Rename</value>
    <value xml:lang='nl'>Hernoemen</value>
  </entry>
  <entry name='ID_8'>
    <value xml:lang='fr'>Ouvrir</value>
    <value xml:lang='en'>Open</value>
    <value xml:lang='nl'>Openen</value>
  </entry>
  <entry name='ID_941'>
    <value xml:lang='fr'>CrC)er</value>
    <value xml:lang='en'>Create</value>
    <value xml:lang='nl'>Aanmaken</value>
  </entry>
</glossary>

My XSL Transformation : 
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <!-- identity template -->
  <xsl:import href="copie.xslt"/>
  <xsl:output method="xml"  encoding="utf-8" indent="yes"/>
  <xsl:variable name="doc2"   select="document('glossaryNL.xml')"/>
  <xsl:template match="entry">
    <xsl:variable name="value_fr" select="current()/value[@xml:lang='fr']"/>
    <xsl:variable name="value_en" select="current()/value[@xml:lang='en']"/>
    <xsl:variable name="value_nl" select="current()/value[@xml:lang='nl']"/>
    <xsl:variable name="no_value_nl" select="string-length ($value_nl) &lt; 1"/>
    <xsl:copy>
      <!-- Copy existing stuff .. -->
      <xsl:apply-templates select="@*|node()"/>
      <xsl:for-each select="$doc2/*/*">
        <!-- If 'fr' and 'en' text elements match  ... -->
        <xsl:if test="    ./value[@xml:lang='fr'] = $value_fr
                      and ./value[@xml:lang='en'] = $value_en">
 
          <xsl:if test="$no_value_nl">
     <!-- If no translation yet, append it ..-->
     <xsl:copy-of select="./value[@xml:lang='nl']"/>
     <xsl:variable name="no_value_nl" select="false"/>
   </xsl:if>
 
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The result I get : 
<STR-Dictionary name="dic000-dial">
  <entry name="Dic_Create">
    <value xml:lang="fr">CrC)er</value>
    <value xml:lang="en">Create</value>
    <value xml:lang="es">Crear</value>
    <value xml:lang="pt">Criar</value>
    <value xml:lang="it">Crea</value>
    <value xml:lang="ja">C$B=EC&KB</value>
    <value xml:lang="de">Neu</value>
    <value xml:lang="zh-TW">C%B;B:C'B+b9</value>
    <value xml:lang="zh-CN">C%B;B:C'B+b9</value>
  <value xml:lang="nl">Aanmaken</value>
<value xml:lang="nl">Aanmaken</value>
</entry>
  <entry name="Dic_Rename">
    <value xml:lang="fr">Renommer</value>
    <value xml:lang="en">Rename</value>
    <value xml:lang="es">Renombrar</value>
    <value xml:lang="pt">Renomear</value>
    <value xml:lang="it">Rinomina</value>
    <value xml:lang="ja">C%BB
C%b0B
$,2>NC%B$b0C&b:B4(B</value>
    <value xml:lang="de">Umbenennen</value>
    <value xml:lang="zh-TW">C&b:B4C%BB
</value>
    <value xml:lang="zh-CN">C&b:B4C%BB
</value>
  <value xml:lang="nl">Hernoemen</value>
</entry>
</STR-Dictionary>

Current Thread