xsl-list
[Top] [All Lists]

[xsl] how to recycle code to process the same element more than once

2009-02-13 06:28:57
Hello everyone,

This is a question about a transformation from a dictionary format that encodes language as an attribute of each term to another (ISO-standard) format in which all terms in each language are grouped under a language element (the language is their parent). I managed to do that, no problem there.

However, I know my code could be improvable at least in one thing: I write the same transformation for each language and in this case it's not too bad because there's only three languages, but that implies I first need to check how many languages there are and that I need to have longer, duplicated code. It would be a pain if there were many languages... (which might be the case in the future).

I tried doing this with a for-each for each term[(_at_)lang='es'] but to no avail (I think I could do it for elements but the problem is that the pivot data is an attribute). I also tried to figure out how that could be done with templates (because in Perl I would do it with a subroutine) but I guess I need to pass the language as a variable and I don't know how to do that.

The question is: how could I write the code that process each langSet only once for all langSets instead of once specifically for each langSet. I hope it's clear.

Now that I've started using XSL more I took a very thick book from the library by a bearded guy ;) but it'll take quite a while to get from that book the knowledge that I need to do this kind of thins. In the meanwhile, thank you so much for your help!

I use processor Saxon 6.5.5.

====================This is the source data (very abridged):=====================

<?xml version="1.0" encoding="ISO-8859-1"?>
<dictionary>
   <author>TERMCAT, Centre de Terminologia</author>
   <title>TO Begudes</title>
   <records>
       <record num="1">
           <term lang="ca">absenta</term>
           <term lang="es">absenta</term>
           <term lang="es">ajenjo</term>
           <term lang="en">absinth</term>
           <term lang="en">absinthe</term>
       </record>
       <record num="2">
           <term lang="ca">acetificació</term>
           <term lang="es">acetificación</term>
           <term lang="en">acetification</term>
       </record>
   </records>
</dictionary>

===============This is the result that I obtain (and should obtain): ==========================

<?xml version="1.0" encoding="UTF-8"?>
<martif type="TBX" xml:lang="en">
  <text>
     <body>
        <termEntry id="1">
           <langSet xml:lang="es">
              <term id="">absenta</term>
              <term id="">ajenjo</term>
           </langSet>
           <langSet xml:lang="en">
              <term id="">absinth</term>
              <term id="">absinthe</term>
           </langSet>
           <!-- (...) more langSets for any other languages -->
        </termEntry>
        <termEntry id="2">
           <langSet xml:lang="es">
              <term id="">acetificación</term>
           </langSet>
           <langSet xml:lang="en">
              <term id="">acetification</term>
           </langSet>
           <!-- (...) more langSets for any other languages -->
        </termEntry>
     </body>
  </text>
</martif>

================This is the stylesheet that I use:=========================

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
   <!--<xsl:output type="xml"/>-->
   <xsl:template match="/">
       <martif type="TBX" xml:lang="en">
           <text>
               <body>
                   <xsl:for-each select="dictionary/records/record">
                       <termEntry id="{(_at_)num}">
                           <xsl:if test="term[(_at_)lang='es']">
                               <langSet xml:lang="es">
                                   <xsl:for-each select="term[(_at_)lang='es']">
                                           <term id="">
<xsl:value-of select="current()" />
                                           </term>
                                   </xsl:for-each>
                               </langSet>
                           </xsl:if>
                           <xsl:if test="term[(_at_)lang='en']">
                               <langSet xml:lang="en">
                                   <xsl:for-each select="term[(_at_)lang='en']">
                                           <term id="">
<xsl:value-of select="current()" />
                                           </term>
                                   </xsl:for-each>
                               </langSet>
                           </xsl:if>
<!-- (...) the same for each language (e.g. ca) -->
                       </termEntry>
                   </xsl:for-each>
               </body>
           </text>
       </martif>
   </xsl:template>
</xsl:stylesheet>

Once again, thank you so much!
Kind regards,
--
Manuel Souto Pico

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>