Removing xmlns attribute from random HTML elements

I had time to get rid of the XSL problem that I have.

Basically, I have a mapped 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 another 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 mainly use XSL to create a JSP file with spring: message tags in it to translate our site.

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

Here is an example 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 tried adding exclude-result prefixes to xsl: stylesheet tags, and although this removes xmlns from the HTML elements, it is added to the spring: tag instead, which will not work if the JSP is parsed (xmlns is an invalid spring: message attribute) . 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 really appreciate it. Sorry if nothing comes of it, I will add it if necessary.

Thanks in advance.

+4
source share
2 answers

You want the result to contain namespace prefixes without namespace declarations for these prefixes. This is not possible in xslt, because such xml output will not be well formed, so you will need to post-process these files, something like this:

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

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

+2
source

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

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

+2
source

Source: https://habr.com/ru/post/1444459/


All Articles