tangofan
tangofan

Reputation: 13

XSL: Eliminate Tag from HTML code

I am trying to eliminate certain tags (script & title) from an HTML file, using XSLT.

This is my source file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script type="text/javascript" src=
"popup_html.js">
</script>
<!-- Redirect browser to frame page if page is not in the content frame. -->

<script type="text/javascript">
//<![CDATA[
<!--
if(top.frames.length==0) { top.location.href="index.html?3_plus_4.htm"; }
else { parent.lazysync('3_plus_4.html'); }
//-->
//]]>
</script>
<script type="text/javascript" src="highlight.js">
</script>
<title>3+4</title>
<body></body>
</html>

And this is my XSLT file:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="" >

<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match = "script"/>
<xsl:template match = "title"/>

</xsl:stylesheet>

And this is the output file (as generated with http://www.freeformatter.com/xsl-transformer.html ):

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
   <script type="text/javascript" src="popup_html.js" />
   <!-- Redirect browser to frame page if page is not in the content frame. -->
   <script type="text/javascript">//
&lt;!--
if(top.frames.length==0) { top.location.href="index.html?3_plus_4.htm"; }
else { parent.lazysync('3_plus_4.html'); }
//--&gt;
//</script>
   <script type="text/javascript" src="highlight.js" />
   <title>3+4</title>
   <body />
</html>

As you can see the output still has the title and script tags. What do I need to change in my XSLT file to get rid of them?

Upvotes: 0

Views: 40

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

Your eliminating templates do not match anything, because the source XML puts all its elements in a namespace.

Try instead:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml">

<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="x:script"/>
<xsl:template match="x:title"/>

</xsl:stylesheet>

Upvotes: 1

Related Questions