0
votes

I want to transform an XML file to xml but I want also some text convert with other element

Input:

<parent> Some parent text
 <child1>child text</child1><child2>child text</child2>
   more parent text</parent>

Desired OutPut:

<parent><text>Some parent text</text>
 <child1>child text</child><child2>child text</child2>
   <text>more parent text</text></parent>

My XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" indent="no"/>
<xsl:template match="node()">
    <xsl:copy>
       <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
<xsl:template match="parent/text()">
     <xsl:element name="node">
         <xsl:apply-templates select="text()"/>
     </xsl:element>
  </xsl:template>
<xsl:template match="child1">
     <xsl:element name="child1">
         <xsl:apply-templates select="node()"/>
     </xsl:element>
  </xsl:template>
<xsl:template match="child2">
     <xsl:element name="child2">
         <xsl:apply-templates select="node()"/>
     </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Is there anyway I can fix this to include some child text with child node?

1

1 Answers

0
votes

First, you need to set the output method to be xml:

<xsl:output method="xml" indent="no"/>

Then by modifying the template processing the text:

<xsl:template match="parent/text()">
     <xsl:element name="text">
         <xsl:value-of select="." />
     </xsl:element>
</xsl:template>

This is my resulting output:

<?xml version="1.0" encoding="UTF-8"?>
<parent>
   <text> Some parent text
 </text>
   <child1>child text</child1>
   <child2>child text</child2>
   <text>
   more parent text</text>
</parent>