How to get converted xml file with all child tags for each parent tag event with only one parent tag

I am using this input xml file.

                 <Content>
                <body><text>xxx</text></body>
                    <body><text>yy</text></body>
               <body><text>zz</text></body>
               <body><text>kk</text></body>
                   <body><text>mmm</text></body>
                        </Content>

after Xslt conversion, the output should be

                        <Content>
                 <body><text>xxx</text>
                       <text>yy</text>
                           <text>zz</text>
                     <text>kk</text>
                   <text>mmm</text></body>
                     </Content>

Can anyone provide their relavant Xsl file.

+3
source share
2 answers

This is a complete conversion :

<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="body"/>
 <xsl:template match="body[1]">
  <body>
   <xsl:apply-templates select="../body/node()"/>
  </body>
 </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document:

<Content>
    <body>
        <text>xxx</text>
    </body>
    <body>
        <text>yy</text>
    </body>
    <body>
        <text>zz</text>
    </body>
    <body>
        <text>kk</text>
    </body>
    <body>
        <text>mmm</text>
    </body>
</Content>

creates the desired, correct result :

<Content>
   <body>
      <text>xxx</text>
      <text>yy</text>
      <text>zz</text>
      <text>kk</text>
      <text>mmm</text>
   </body>
</Content>

Explanation

  • an identification rule copies each node "as is".

  • Ends with two patterns. The first ignores / deletes each item body.

  • , , ( body) body, body . body body , body ( body body) .

+2
    <xsl:template match="Content">
      <body>
            <xsl:apply-templates select="body/text"/>
      </body>
    </xsl:template>

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

All Articles