2
votes

I want to transform an XML file to text but I want also some elements not to be transformed.

e.g Input:

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

expected output:

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

my current 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="child1">
         <xsl:element name="child1">
             <xsl:apply-templates select="node()"/>
         </xsl:element>
      </xsl:template>

  </xsl:stylesheet>

But what what I get is:

Some parent text child text more parent text

is there anyway I can fix this to include some child elements?

1
If you want XML output then why are you using method="text"?Ian Roberts
I can try that but as you can see the expected output xml elements are in the middle of texts.asahun
I just tried xml too but not making any difference.asahun
Is it the case that you don't want the parent element to appear in the output, but everything else stays the same?Tim C
not exactly other child elements will be transformed but yes only text part of parent node will be included.asahun

1 Answers

2
votes

This XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>

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

  <xsl:template match="parent">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>

Applied to this XML input document:

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

Will yield the requested (not well-formed XML) output document:

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

Of course you can also match child1 elements:

  <xsl:template match="child1">
    <xsl:element name="child1_NEW">
      <xsl:apply-templates select="node()"/>
    </xsl:element>
  </xsl:template>

And handle them differently:

 Some parent text
     <child1_NEW>child text</child1_NEW>
       more parent text