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

Subject: Re: [xsl] Trying to ignore specific tags with no content
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 Jun 2011 14:57:08 -0400
Leave your existing template as it is and add:

<xsl:template match="strong[normalize-space(.) = '']" mode="inline"/>

Note that the marks between the "=" and "]" are two single-quotes.

Due to the non-trivial match pattern, this will be a higher-priority
template than your existing one, so it will override it for the cases
that it matches.  The normalize-space() function will trim whitespace
from the start and end and collapse it in the middle, so it's very
handy for "text content other than whitespace" tests.

-Brandon :)

On Thu, Jun 2, 2011 at 12:24 PM, Paul Chernoff
<pchernoff@xxxxxxxxxxxxxxxxx> 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
> 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.
> 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:
>            <Main_Body>
>                <strong>For the Quads</strong>
>            </Main_Body>
>            <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
>                repeat.</Main_Body>
> Here is a snippet of the XSLT code:
>        <xsl:template match="SubHead1" mode="inline">
>                <h4 class="headline"><xsl:apply-templates
>        </xsl:template>
>        <xsl:template match="SubHead2" mode="inline">
>                <h5 class="subheadline"><xsl:apply-templates
>        </xsl:template>
>        <xsl:template match="Main_Body" mode="inline">
>                        <p><xsl:apply-templates mode="inline"/></p>
>        </xsl:template>
>        <xsl:template match="strong" mode="inline">
>                <strong><xsl:apply-templates mode="inline"/></strong>
>        </xsl:template>
>        <xsl:template match="em" mode="inline">
>                <em><xsl:apply-templates mode="inline"/></em>
>        </xsl:template>
>        <xsl:template match="a" mode="inline">
>                <xsl:copy-of select="." copy-namespaces="no"/>
>        </xsl:template>
>        <xsl:template match="br" mode="inline">
>                <br/>
>        </xsl:template>
> 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.
> Or perhaps my approach is just wrong.
> Paul Chernoff
> Director of Information Technology
> Washingtonian Magazine

Current Thread