Re: [xsl] Removing nodes with text content ?

Subject: Re: [xsl] Removing nodes with text content ?
From: Torsten Schassan <schassan@xxxxxx>
Date: Wed, 03 Aug 2011 17:25:03 +0200
Hi Fabien,

as Martin's answer didn't let you get rid of the line breaks,

I would like to remove the intermediary<phrase>  nodes to avoid line
breaks (which are highly desirable on other places, but here are
annoying me), and thus having everything on a single line (using the
docbook stylesheets, already customized).

a very specific, thus "inelegant" full "answer" below, but some additional hints anyway:


- to avoid line breaks in the output using normalize-space() is often useful
- xsl:strip-space might help as well.


<xsl:template match="phrase[@role='rtf']">
    	<xsl:copy>
    		<xsl:apply-templates select="./*"/>
    	</xsl:copy>
</xsl:template>

1. If you want to remove the element, you shouldn't copy it. (On the other hand, the template for para lacks this.)


2. Why wasn't text in the output? Your select selects only element nodes. In the more general template you use correctly "node()".

This should be working then (XSLT1):

<xsl:output indent="yes"/>

<xsl:template match="para">
<xsl:copy>
<phrase>
<xsl:if test=".//phrase[@role='rtf']">
<xsl:attribute name="role">rtf</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</phrase>
</xsl:copy>
</xsl:template>
<xsl:template match="phrase[@role='rtf']" >
<xsl:apply-templates select="node()"/>
<xsl:if test="following-sibling::phrase"><xsl:text> </xsl:text></xsl:if>
</xsl:template>


    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

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


Best, Torsten


--
Torsten Schassan
Digitale Editionen
Abteilung Handschriften und Sondersammlungen
Herzog August Bibliothek, Postfach 1364, D-38299 Wolfenbuettel
Tel.: +49-5331-808-130 (Fax -165), schassan {at} hab.de

http://www.hab.de/forschung/projekte/europeana-regia.htm
http://www.hab.de/forschung/projekte/weiss64.htm

Current Thread