XSLT_TAO/filtres_TMX_pour_Calc/TMX-export.xsl

113 lines
5.0 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8"?>
<!-- Dominique Meeùs, created 1-10-2013, version 0.9.
XSLT transformation of an Open Document Format spreadsheet in five columns
into a TMX translation memory exchange file.
Filter to install as an export filter for LibreOffice Calc. -->
<!-- Dominique Meeùs, modified 21-5-2020, version 0.92.
Hardcoded languages of the columns are : nl-BE, fr-BE, de-DE, en-GB, es-ES. -->
<!-- Philippe Tourigny, modified 12-9-2022, version 0.94
Enable the filter to read the language code from the first column
in each row. Also, add a SYSTEM DOCTYPE declaration to the output
XML file.-->
<!-- Copyright 2013, 2020 Dominique Meeùs, and 2022 Philippe Tourigny@.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program.
If not, see http://www.gnu.org/licenses/. -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
exclude-result-prefixes="office table text">
<!-- Namespaces needed to access parts of the document -->
<!-- Version 0.94: Add a SYSTEM DOCTYPE to output -->
<xsl:output method = "xml" indent = "yes" encoding = "UTF-8"
doctype-system="tmx14.dtd" omit-xml-declaration = "no"/>
<xsl:template match="/">
<tmx version="1.4">
<!-- Define variables to make code easier to manage.
Cells in the first row are language code headings.
The first target language is selected as the
administrative language for the output TMX.
The first column identifies the source language. -->
<xsl:variable name="headingCell"
select="//table:table/table:table-row[1]/table:table-cell"/>
<xsl:variable name="adminlang"
select="$headingCell[2]/text:p"/>
<xsl:variable name="srclang"
select="$headingCell[1]/text:p"/>
<!-- Define the TMX header
The <xsl:attribute> element is used because variables
are not recognized if entered directly in attributes. -->
<header>
<xsl:attribute name="creationtool">TMX-export for LibreOffice</xsl:attribute>
<xsl:attribute name="creationtoolversion">0.94</xsl:attribute>
<xsl:attribute name="segtype">sentence</xsl:attribute>
<xsl:attribute name="o-tmf">application/vnd.oasis.opendocument.spreadsheet</xsl:attribute>
<xsl:attribute name="adminlang">
<xsl:value-of select="$adminlang"/>
</xsl:attribute>
<xsl:attribute name="srclang">
<xsl:value-of select="$srclang"/>
</xsl:attribute>
<xsl:attribute name="datatype">plaintext</xsl:attribute>
</header>
<!-- Define the TMX body
Additional templates are called to make the code
easier to modify and maintain. -->
<body>
<!-- Call the template to insert <tu> elements -->
<xsl:apply-templates select="//table:table"/>
</body>
</tmx>
</xsl:template>
<!-- Templates to populate the <tu> and <tuv> elements-->
<!-- Template for <tu> elements:
Loop through each row, skipping the first one. -->
<xsl:template match="table:table">
<xsl:for-each select="table:table-row">
<tu>
<xsl:apply-templates select="//table:table-row[position()>1]"/>
</tu>
</xsl:for-each>
</xsl:template>
<!-- Template for the <tuv> elements:
Loop through each cell in the row, and assign the value
of the language for that column to the xml:lang attribute.
After that, add the <seg> element and populated it with the
text in the current cell. -->
<xsl:template match="table:table-row[position()>1]">
<xsl:for-each select="table:table-cell">
<xsl:variable name="currentLang"
select="//table:table/table:table-row[1]/table:table-cell"/>
<xsl:variable name="currentColumn"
select="position()"/>
<xsl:if test="normalize-space(text:p) != ''">
<tuv>
<xsl:attribute name="xml:lang">
<xsl:value-of select="$currentLang[$currentColumn]/text:p"/>
</xsl:attribute>
<seg>
<xsl:value-of select="text:p"/>
</seg>
</tuv>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>