109 lines
4.8 KiB
XML
109 lines
4.8 KiB
XML
<?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.95
|
||
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. -->
|
||
<!-- Philippe Tourigny, modified 23-4-2023, version 0.96
|
||
Refactor code to use templates correctly and clean up comments. -->
|
||
<!-- Philippe Tourigny, modified 27-5-2023, version 0.97
|
||
Skip empty rows to avoid creating empty `<tuv>` elements. -->
|
||
<!-- 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/. -->
|
||
|
||
<!-- Declare the namespaces needed to access parts of the document. -->
|
||
<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">
|
||
|
||
<!-- Define variables to make code easier to manage.
|
||
The first row contains the language code headings, with
|
||
the source language in the first column. The target language
|
||
in the second column is used as the administrative 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"/>
|
||
|
||
<!-- Since version 0.93: 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 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.97</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 and use templates to populate
|
||
the <tu> and <tuv> elements. -->
|
||
<body>
|
||
<!-- Apply template to insert <tu> elements -->
|
||
<xsl:apply-templates select="//table:table-row[position()>1]"/>
|
||
</body>
|
||
</tmx>
|
||
</xsl:template>
|
||
|
||
<!-- Populate the <tu> elements starting at the second row. -->
|
||
<xsl:template match="table:table-row[position()>1]">
|
||
|
||
<!-- Skip empty rows -->
|
||
<xsl:if test="table:table-cell/text:p">
|
||
<tu>
|
||
<xsl:apply-templates select="table:table-cell"/>
|
||
</tu>
|
||
</xsl:if>
|
||
</xsl:template>
|
||
|
||
<!-- Populate the <tuv> elements. -->
|
||
<xsl:template match="table:table-cell">
|
||
|
||
<!-- Get the language from the column heading -->
|
||
<xsl:variable name="currentLang" select="$headingCell"/>
|
||
<xsl:variable name="currentColumn" select="position()"/>
|
||
|
||
<!-- Make sure the cell is not empty -->
|
||
<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:template>
|
||
</xsl:stylesheet> |