xsltsymphony-cms

XSL: Is there an easy way to prevent widows? (part 2)


Using the solution from XSL: Is there an easy way to prevent widows? makes odd tags in the DOM.

Is there a way to make it not insert an element of the name? Currently if I run

<xsl:apply-templates select="solution-headline" mode="widow-fix" /> 

it will insert

<solution-headline>Lorem ipsum<solution-headline/> 

I want it to insert

<xsl:text>Lorem ipsum<xsl:text/>

Solution

  • If you just want the text then the simplest approach would be to apply the widow-fix templates to just the text node children of the solution-headline element rather than to the element itself:

    <xsl:apply-templates select="solution-headline/text()" mode="widow-fix" />
    

    If you always want the widow-fixing to give you just the text and not include the surrounding element, then delete the existing template

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

    Now when you apply the widow-fix templates to solution-headline it will use the default template which essentially just does <xsl:apply-templates mode="widow-fix" /> (i.e. process all child nodes using the same mode) without the copy, and you'll get all the descendant text nodes processed by the widow-fixing template.