RE: [xsl] replacing values in file1 from file2

Subject: RE: [xsl] replacing values in file1 from file2
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Thu, 25 Mar 2004 07:52:36 -0700
A couple of changes to a solution I posted a few days ago should give
you exactly what you want...

This XSL >>

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:variable name="sourceDoc" select="document('file_1.xml')"/>
<xsl:variable name="translationDoc" select="document('file_2.xml')"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <xsl:variable name="groupTranslation">
    <xsl:element name="translations">
      <xsl:apply-templates select="$sourceDoc/file1/terms">
        <xsl:with-param name="translationDoc"
select="$translationDoc/file2/translated"/>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:variable>
  <xsl:copy-of select="$groupTranslation"/>
</xsl:template>

<xsl:template match="terms">
  <xsl:param name="translationDoc"/>
  <xsl:variable name="pos" select="position()"/>
    <xsl:element name="tranlatedTerm">
      <xsl:element name="source">
        <xsl:attribute name="lang"><xsl:value-of
select="source[position() = 1]/@lang"/></xsl:attribute>
        <xsl:value-of select="source[position() = 1]/term"/>
      </xsl:element>
      <xsl:element name="translation">
        <xsl:attribute name="lang"><xsl:value-of
select="$translationDoc/term[position() = $pos]/@lang"/></xsl:attribute>
        <xsl:value-of select="$translationDoc/term[position() = $pos]"/>
      </xsl:element>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

Outputs this XML >>

<?xml version="1.0" encoding="UTF-8"?>
<translations>
  <tranlatedTerm>
    <source lang="english">dog</source>
    <translation lang="danish">hund</translation>
  </tranlatedTerm>
  <tranlatedTerm>
    <source lang="english">dog</source>
    <translation lang="danish">bord</translation>
  </tranlatedTerm>
</translations>

Now just convert the $groupTranslation variable to a nodeset and you can
use apply-templates to transform it or just store it as is as an XML
file for later use.

Best of luck!

<M:D/>



-----Original Message-----
From: ronan martin [mailto:ronanmartin124@xxxxxxxxxxx] 
Sent: Thursday, March 25, 2004 4:46 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] replacing values in file1 from file2

Hi

I've been stuck on this - I thought it would be simpler than it is. (I'm

pretty much only just past beginner level)
I have two xml files. One with pairs of terms in source and target
language 
but with an untranslated target language term (say, English and Danish).
In 
another xml file I have the Danish translations, one for each pair of
terms, 
and sequenced in exactly the same way.

<file1>
  <terms>
    <source lang="english">
      <term>dog</term>
    </source>
    <source lang="danish">
      <term>dog</term>
    </source>
  </terms>
  <terms>
    <source lang="english">
      <term>dog</term>
    </source>
    <source lang="danish">
      <term>dog</term>
    </source>
  </terms>
</file1>

<file2>
  <tranlated>
    <term lang="danish">hund</term>
    <term lang="danish">bord</term>
  </translated>
</file2>

I've tried matching a nodeset in file1 using <..  
match="//source[@lang='danish']/term"> seems fine. Then I've tried to 
copy-of the value from select="document('file2.xml')//term/text()" in 
various ways. I get the first value only from the list copied, but in
all 
the locations I wanted. How do I pass on the number of the node in the
first 
nodeset to the nodeset selected from the second file. I tried setting up
a 
variable using position() but couldn't get it to work.
Sorry this is so long-winded. I simplified the xml. In reality I'm using
tmx 
which is a translation memory format.

thanks
Ronan

_________________________________________________________________
Fe alle de nye og sjove ikoner med MSN Messenger http://messenger.msn.dk

Current Thread