[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Removing <div/> tags from HTML

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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.