DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • Build a Simple Chat Server With gRPC in .Net Core
  1. DZone
  2. Coding
  3. Languages
  4. How to Handle Heavily Nested XML Tags with XSLT

How to Handle Heavily Nested XML Tags with XSLT

Nick Watts user avatar by
Nick Watts
·
Oct. 15, 12 · Interview
Like (0)
Save
Tweet
Share
12.63K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

XSLT XML

Published at DZone with permission of Nick Watts, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • Build a Simple Chat Server With gRPC in .Net Core

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: