[xsl] Removing <div/> tags from HTML

Subject: [xsl] Removing <div/> tags from HTML
From: "Mark Peters markpeters.work@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 19 May 2014 20:23:32 -0000
Hi,

I'm trying to convert documentation from HTML to another markup language.
The documentation includes content nested in many various levels of <div/>
tags.

For example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>Title</h1>
        <div>
            <div>
                    <p>Text</p>
                        <div>
                            <ul>
                                <li>List item</li>
                                <li>List item</li>
                                <li>List item</li>
                            </ul>
                        </div>
                    <p>Text</p>
                    <h2Title</h2>
                    <h3>Title</h3>
                    <p>Text.</p>
                    <div>
                        <div>
                            <p>Text</p>
                         <div>
                    <div>
               <div>
            <div>
     </body>
</html>

I simply want to remove all <div/> tags but retain the child nodes and
attributes for each. I'm also removing the <html/> nodes but retaining the
children, and removing the <head/> node altogether. But I can figure out
how to accomplish those goals.

The desired output would look like this:

    <body>
        <h1>Title</h1>
         <p>Text</p>
         <ul>
              <li>List item</li>
              <li>List item</li>
              <li>List item</li>
          </ul>
          <p>Text</p>
          <h2Title</h2>
           <h3>Title</h3>
           <p>Text.</p>
            <p>Text</p>
     </body>

Here's current stylesheet. I've tried to remove the <div/> tags several
different ways, using identity templates and for-each elements.

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

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

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

    <xsl:template match="body">
        <xsl:element name="body">
            <xsl:for-each select="//node()">
                <xsl:choose>
                    <xsl:when test="self::div">
                        <xsl:apply-templates select="node()"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="self::node()|node()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

With this current stylesheet, I wanted to test each child node under
<body/>. For each <div/> node, the stylesheet would copy the child nodes
and attributes only. For all other nodes, the stylesheet would copy the
current node and all children. But my output is blank.

Thanks in advance for any help.

Mark

Current Thread