user1633701
user1633701

Reputation: 1

Inserting new element with specifiv attributes

here is an excerpt of the source xml:

 <text key="#OK" default="TEST">
        <lang id="de" value="i.O." />
        <lang id="en" value="OK" />
        <lang id="cz" value="ak" />
        <lang id="dk" value="OK" />
 </text>

I would like to transform this document so that within each of the text elements a new lang element with the id attribute "ch" and a value attribute with the content of the default attritbute of the text element is inserted.

The result should look like this:

 <text key="#OK" default="TEST">
      <lang id="de" value="i.O." />
      <lang id="en" value="OK" />
      <lang id="cz" value="ak" />
      <lang id="dk" value="OK" />
      <lang id="ch" value="TEST" />
 </text>

Any push in the right direction is very, very much appreciated.

Upvotes: 0

Views: 41

Answers (1)

JLRishe
JLRishe

Reputation: 101748

This will do it:

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

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

  <xsl:template match="text">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <lang id="ch" value="{@default}" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input, the result is:

<text key="#OK" default="TEST">
  <lang id="de" value="i.O." />
  <lang id="en" value="OK" />
  <lang id="cz" value="ak" />
  <lang id="dk" value="OK" />
  <lang id="ch" value="TEST" />
</text>

Upvotes: 1

Related Questions