.NET Zone is brought to you in partnership with:

I am a lifelong student of computer science, music, and literature. In pursuit of those interests, I work by day as a programmer Chemical Abstracts Services, moonlight as the creator and curator of Mashed Code Magazine. review books for The Pragmatic Bookshelf and Manning and listen to a fascinating collection of music while performing all of my duties. I have specialized in working with web services and mastering various testing techniques and tools. I am finally done with formal education, having a B.A. in English from The Ohio State University and a M.S. in Computer Science from Franklin University. Nick is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

How to Handle Heavily Nested XML Tags with XSLT

10.14.2012
| 4054 views |
  • submit to reddit

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 Nick Watts, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)