Re: [xsl] Trying to ignore specific tags with no content

Subject: Re: [xsl] Trying to ignore specific tags with no content
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 02 Jun 2011 19:01:12 +0200
Paul Chernoff wrote:
Despite doing XSLT work for the past few years I still feel like a novice. I have hit the wall on a specific problem which I should think would be easy to solve.

I am processing a XML file that I want to convert to HTML. Everything works fine unless there is an empty<strong />  tag in it. This happens because the source document originated from Adobe InCopy and in cutting a pasting a bold style that is applied to the space between characters is left in the document. My XSLT ends up creating a<strong />  tag in this case which is disliked by web browsers, they see this as a "begin strong tag" resulting in bolding that is never turned off.

If you use XSLT 1.0 then you should consider to have the stylesheet output HTML 4 with <xsl:output method="html"/>, in that case the XSLT processor is certainly not going to use XML syntax in the form of <strong /> for empty result elements, rather it will use HTML syntax in the form of <strong></strong>.
If you use XSLT 2.0 you have the choice between output method="html" for HTML 4 output or output method="xhtml" for XHTML 1.0 output, both to be served as text/html meaning both times the XSLT processor will ensure that the syntax of any serialized result elements is understood by an HTML parser. That way you won't get <strong /> either.

My XSLT is taking a intermediary XML file, not the XML generated directly from InCopy.

When I get to processing the contents of the document I use a bunch of xsl:template tags. I never know if any paragraphs will have strong, em, or br tags within it.

Here is a snippet of the XML document being processed:
                 <strong>For the Quads</strong>
             <Main_Body>Place one foot on a step or bench so the quadricep is parallel to the floor.
                 Place the Stick on the top of the quadricep and slowly roll it on top of the muscle
                 while applying pressure. Continue rolling for about 30 seconds. Switch legs and

Here is a snippet of the XSLT code:

	<xsl:template match="SubHead1" mode="inline">
		<h4 class="headline"><xsl:apply-templates mode="inline"/></h4>
	<xsl:template match="SubHead2" mode="inline">
		<h5 class="subheadline"><xsl:apply-templates mode="inline"/></h5>
	<xsl:template match="Main_Body" mode="inline">
			<p><xsl:apply-templates mode="inline"/></p>
	<xsl:template match="strong" mode="inline">
		<strong><xsl:apply-templates mode="inline"/></strong>
	<xsl:template match="em" mode="inline">
		<em><xsl:apply-templates mode="inline"/></em>
	<xsl:template match="a" mode="inline">
		<xsl:copy-of select="." copy-namespaces="no"/>
	<xsl:template match="br" mode="inline">

My problem is that if 'match="strong"' and if the string of the strong tag is empty, I don't want to generate any results. I have attempted to use the xsl:if tag but it doesn't work because I am already in the contents of the<strong> tags. I have been attempting to put in some sort of conditional statement in the xsl:template when match="strong" to say 'if there is no content don't do anything'. I just can't think of any way of writing that.

Well adding <xsl:template match="strong[not(node())]"/> would fix it.

Or perhaps my approach is just wrong.

I think using the proper output method should avoid the problem.


	Martin Honnen --- MVP Data Platform Development

Current Thread