[xsl] For each child, create a new parent node including only that child

Subject: [xsl] For each child, create a new parent node including only that child
From: "Manuel Souto Pico terminolator@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 28 Aug 2019 10:53:17 -0000
Dear all,

What I am trying to do should be quite easy but I need some help.

Here you can see the kind of input I have and my first attempt:
https://xsltfiddle.liberty-development.net/6rewNxx

So:

- tu
--   tuv en (1)
--   tuv pt (1)
--   tuv pt (2)
--   tuv pt (n)

In that input you can see that a tu node might have one source language tuv
child and more than one target-language tuv children, such as the one with
source text "Moving Around and Tracking Progress".

The expect output is to have only one tuv child per tu node:

- tu
--   tuv en (1)
--   tuv pt (1)

- tu
--   tuv en (1)
--   tuv pt (2)

- tu
--   tuv en (1)
--   tuv pt (1)

Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tmx SYSTEM "
https://raw.githubusercontent.com/moiatgit/omegat/master/src/schemas/tmx14.dt
d
">

      <tu>
         <tuv xml:lang="en-ZZ">
            <seg>Moving Around and Tracking Progress</seg>
         </tuv>
         <tuv xml:lang="pt-BR">
            <seg>MovimentaC'C#o e monitoramento de progresso</seg>
         </tuv>
      </tu>

      <tu>
         <tuv xml:lang="en-ZZ">
            <seg>Moving Around and Tracking Progress</seg>
         </tuv>
         <tuv xml:lang="pt-BR">
            <seg>Movendo-se e Monitorando o seu progresso</seg>
         </tuv>
      </tu>

      <tu>
         <tuv xml:lang="en-ZZ">
            <seg>Moving Around and Tracking Progress</seg>
         </tuv>
         <tuv xml:lang="pt-BR">
            <seg>Movendo-se e Monitorando seu progresso</seg>
         </tuv>
      </tu>

In other words, unmerge the tu node, keeping always the same source text.

My stylesheet is:

<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:strip-space elements="*"/>

    <xsl:template match="body">
        <xsl:copy>
            <xsl:for-each select='tu/tuv[@xml:lang="pt-BR"]'>
                <tu>
                    <xsl:copy-of select='ancestor::tuv[@xml:lang="en-ZZ"]'/>
                    <xsl:copy-of select="."/>
                </tu>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

It has several problems:

1. The tmx root node is missing in my output
2. The source language (en-ZZ) does not appear in my output
3. It is specific for pt-BR, but the tag for the target language (i.e. for
the multiple children) could be any -- the stylesheet should be generic
(target-language agnostic).

I don't have a preference about the XSLT version, I think there are
non-commercial (free to use) Saxon processors for all three.

Hopefully somebody can help me find solutions or point me in the right
direction. Thanks in advance.

Cheers, Manuel

Current Thread