Platinum Partner
dotnet,architects,xml,tips and tricks,.net & windows

How to Handle Heavily Nested XML Tags with XSLT

I’m new to using Extensible Stylesheet Language Transformations (XSLT). Because I’m new and I had a fairly complex (or so I thought) task to perform with XSLT, I was confused at first.

The task I had and, consequently, the issue that was tripping me up, was with transforming heavily nested tags in my source XML file. Here’s an example of the type of XML I’m talking about.

<?xml version="1.0" encoding="UTF-8"?>
<tag1>
  <tag1>
    <tag1>
      <tag1>
        <arbitrary></arbitrary>
        <tag2>
          <tag2>
            <tag1>
              <texttag></texttag>
            </tag1>
          </tag2>
        </tag2>
      </tag1>
    </tag1>
  </tag1>
</tag1>

The gist of this example is that tag1 and tag2 can be nested within each other to arbitrary depths.  At any level of this hierarchy, there can exist other various tags.

The task I had was to transform the XML above to the following:

<?xml version="1.0" encoding="UTF-8"?>
<tag_start/>
  <tag_start/>
    <tag_start/>
      <tag_start/>
        <arbitrary></arbitrary>
        <tag2_start/>
          <tag2_start/>
            <tag1_start/>
              <texttag></texttag>
            <tag1_end/>
          <tag2_end/>
        <tag2_end/>
      <tag1_end/>
    <tag1_end/>
  <tag1_end/>
<tag1_end/>

A little bit of an odd format, but that was, nonetheless, the requirement. The structure is basically the same as before except that there are explicit tags for the start and the end of the tag (for tag1 and tag2) and all other tags placement and content are preserved.

Because of the oddball structure, this seems like an immensely difficult task at first. I went down a lot of paths with my XSLT that tried to do what amounted to a recursive approach in programming, which never worked. It turned out that I misunderstood how an XSLT engine works. I finally stumbled on a great explanation on Stack Overflow that helped: http://stackoverflow.com/a/6199369.

With that explanation in hand, I was able to write this XSLT file to do the transformation:

<?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"/>

    <xsl:template match="//tag1" >
       <tag1_start/>
       <xsl:apply-templates/>
       <tag1_end/>
    </xsl:template>
    
    <xsl:template match="//tag2">
       <tag2_start/>
       <xsl:apply-templates/>
       <tag2_end/>
    </xsl:template>
  
    
    <!-- 
         standard copy template, this copies all nodes 
         and attributes from the original to the transformed XML.
     -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template> 

</xsl:stylesheet>

I really can’t state things better than the Stack Overflow user did, so I won’t try. Hopefully, if you’re having a similar problem, you will find this post and it will help cajole you into finding a solution.

Curator's note: If you're looking for an XML, SQL, and UML toolkit, check out MissionKit from our sponsor Altova. 

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}