0

I searched for a solution on the internet but didn´t find one. I´m not even sure if this is possible or what causes this behaviour. I´m trying to copy a node content to a nother node. It works without a problem, but then in the first node is a tag (xliff:g), only the tag text element is copied.

What i´m missing, or why at all this tag is not copied ?

Here is my input xml:

<?xml version="1.0"?>
<texts xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
 <?xml-stylesheet href="hello.xsl" type="text/xsl"?>
  <strings>
    <string name="cpu">
        <en>example1</en>
        <de name="cpu">"something <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g> something1</de>
        <es>""</es>
    </string>
    <string name="gpu">
        <en>example2</en>
        <de name="gpu">something2<xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g></de>
        <es>""</es>
    </string>
    <string name="mainboard">
        <en>example3</en>
        <de><xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g></de>
        <es>""</es>
    </string>
</strings>
</texts>

My xsl to transform the xml:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>

    <xsl:template match="strings/string/es/text()">
     <xsl:value-of select="../../de"/>
    </xsl:template>

</xsl:stylesheet>

And the output where only the text value of the xliff:g tag is copied:

<texts xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><?xml-stylesheet href="hello.xsl" type="text/xsl"?>
   <strings>
      <string name="cpu">
         <en>example1</en>
         <de name="cpu">"something <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g> something1</de>
         <es>"something %1$s something1</es>
      </string>
      <string name="gpu">
         <en>example2</en>
         <de name="gpu">something2<xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g>
         </de>
         <es>something2%1$s</es>
      </string>
      <string name="mainboard">
         <en>example3</en>
         <de>
            <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g>
         </de>
         <es>%1$s</es>
      </string>
   </strings>
</texts>

The desired output would look like this:

<texts xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><?xml-stylesheet href="hello.xsl" type="text/xsl"?>
   <strings>
      <string name="cpu">
         <en>example1</en>
         <de name="cpu">"something <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g> something1</de>
         <es>"something <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g> something1</es>
      </string>
      <string name="gpu">
         <en>example2</en>
         <de name="gpu">something2<xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g>
         </de>
         <es>something2<xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g></es>
      </string>
      <string name="mainboard">
         <en>example3</en>
         <de>
            <xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g>
         </de>
         <es><xliff:g id="WIDGET_HOST_NAME">%1$s</xliff:g></es>
      </string>
   </strings>
</texts>

Maybe someone could help me figuring out what i´am doing wrong, or why the transformation behave like this.

Many thanks in advance Cali

Cali
  • 35
  • 1
  • 6

2 Answers2

1

<xsl:value-of select="../../de"/> creates a text node with the string value of the first selected node, if you want to copy the node use <xsl:copy-of select="../../de"/>, or you probably want <xsl:copy-of select="../../de/*"/> to copy the child element(s) of the de element.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

Instead of:

<xsl:value-of select="../../de"/>

use:

<xsl:copy-of select="../../de/node()"/>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51