[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: Removing
tags from HTML


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

Never mind. Figured it out.

<?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="div">
      <xsl:apply-templates />
  </xsl:template>

</xsl:stylesheet>

Regards,
Mark


On Mon, May 19, 2014 at 4:23 PM, Mark Peters <markpeters.work@xxxxxxxxx>wrote:

> 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
>
>
>
>


-- 
Senior technical writer
Pivotal


Current Thread
Keywords