JFranks
JFranks

Reputation: 43

Removing xmlns attribute from random HTML elements

I've been having a heck of a time getting rid of an XSL problem that I have.

Basically, I have a matched template that calls a named template in another XSL file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:include href="/_internal/stylesheets/core/common" />

    <xsl:template match="system-page">

        <div id="main">
        <div class="pageHeading">
            <h1><system-page-display-name />&#160;</h1>
            <xsl:if test="current()/dynamic-metadata[name='Printable']/value='true' or current()/dynamic-metadata[name='Shareable']/value='true'">
                <xsl:call-template name="shareAndPrint">
                    <xsl:with-param name="shareable" select="current()/dynamic-metadata[name='Shareable']/value" />
                    <xsl:with-param name="printable" select="current()/dynamic-metadata[name='Printable']/value" />
                </xsl:call-template>
            </xsl:if>
        </div>
        <xsl:copy-of select="current()//system-data-structure/html/node()"/>
    </div>


    </xsl:template>

Then, in the other file, here is the template that I call:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:spring="http://www.springframework.org/tags" version="1.0">

<xsl:template name="shareAndPrint">

    <xsl:param name="shareable"/>
    <xsl:param name="printable"/>

    <div class="shareBar">
        <xsl:if test="$printable = 'true'">
            <a class="print" href="javascript:window.print();"><spring:message code="print.label" /></a>
        </xsl:if>
        <xsl:if test="$shareable = 'true'">
            <span class="st_sharethis" id="shareThis"></span>
        </xsl:if>
    </div>

    <xsl:if test="$shareable = 'true'">
        <script type="text/javascript">$('#shareThis').attr('displayText','ShareThis');</script>
        <script src="http://w.sharethis.com/button/buttons.js" type="text/javascript"></script>
    </xsl:if>

</xsl:template>

As you can see I'm basically using XSL to generate a JSP file that has spring:message tags in it for translating our site.

And my problem is basically that no matter what I do, the XML output always ends up having HTML elements that have xmlns:spring="http://www.springframework.org/tags" on them. I've seen a lot of other posts related to this kind of problem but it seems none of the solutions work for me.

Here is some sample output:

<div id="main">
    <div class="pageHeading"><h1>CR-HTML-Static-WRS-en  - test&#160;</h1>
        <div class="shareBar" xmlns:spring="http://www.springframework.org/tags">
            <a class="print" href="javascript:window.print();">
                <spring:message code="print.label" />
            </a>
            <span class="st_sharethis" id="shareThis" ></span>
        </div>
        <script type="text/javascript" xmlns:spring="http://www.springframework.org/tags">$('#shareThis').attr('displayText','ShareThis');</script>
        <script src="http://w.sharethis.com/button/buttons.js" type="text/javascript" xmlns:spring="http://www.springframework.org/tags" ></script>
     </div>TEST CR</div>

I've tried adding exclude-result-prefixes on the xsl:stylesheet tags and while this removes the xmlns from the HTML elements, it then gets added to the spring:message tag instead, which won't work when the JSP is parsed (xmlns is invalid attribute of spring:message). So I'm not sure what I'm doing wrong or what else I can try.

Please, if anyone has any ideas or solutions for this I would greatly appreciate it. Sorry if I left anything out, I will add it if needed.

Thanks in advance.

Upvotes: 4

Views: 1375

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

Just add exclude-result-prefixes="spring" to the xsl:stylesheet element.

By default, literal result elements such as <div> are copied to the result document along with all in-scope namespaces. The exclude-result-prefixes attribute suppresses this, provided that the namespace is not actually used in an element or attribute name. You'll still get the namespace declaration on the spring:message element itself, but presumably this is wanted.

Upvotes: 2

Petr Kozelka
Petr Kozelka

Reputation: 8000

You want the result to contain namespace prefixes without namespace declarations for these prefixes. This is not possible with xslt, because such xml output would not be wellformed - so you will need to do a post-processing on these files, something like this:

sed -i 's# xmlns:spring="[^"]*"##g' output.html

Note: there is theoretically an option to disable output escaping - but it's too ugly and makes your xslt unmaintainable very quickly. I do not recommend this.

Upvotes: 2

Related Questions