Subject: Re: [xsl] Remove element that all its descendants have no text nodes From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 10 Feb 2010 17:46:38 -0500 |
I want to remove span elements that have empty text nodes and no further descendants. In case the p has ONLY empty spans I want to remove it alltogether.
Example:
<p dir="rtl"><span id="textStyle10"></span></p> <p dir="rtl"><span id="textStyle10">Some text.</span> <span id="textStyle10"></span></p> <p dir="rtl"><span id="textStyle10"></span><sup>1</sup></p> <p dir="rtl"><span id="textStyle10"><br /><br /></span></p>
Desired output: <p dir="rtl"><span id="textStyle10">Some text.</span> </p> <p dir="rtl"><sup>1</sup></p> <p dir="rtl"><span id="textStyle10"><br /><br /></span></p>
In case it is too complicated I can live with such output (remove p that all its spans have empty text nodes): <p dir="rtl"><span id="textStyle10">Some text.</span> <span id="textStyle10"></span></p> <p dir="rtl"><span id="textStyle10"></span><sup>1</sup></p> <p dir="rtl"><span id="textStyle10"><br /><br /></span></p>
I tried the following: <xsl:template match="p[span[normalize-space(.)='']]"/> but it removes the p even if only one span is empty.
<xsl:template match="p[span[normalize-space(.)='' and not(*)] and not(span[normalize-space() or *]) and not(*[not(self::span)])]"/>
<xsl:template match="p[span[normalize-space(.)='' and not(*)] and not(span[normalize-space() or *]) and not(* except span)]"/>
T:\ftemp>type israel.xml <test> <p dir="rtl"><span id="textStyle10"></span></p> <p dir="rtl"><span id="textStyle10">Some text.</span> <span id="textStyle10"></span></p> <p dir="rtl"><span id="textStyle10"></span><sup>1</sup></p> <p dir="rtl"><span id="textStyle10"><br /><br /></span></p> </test> T:\ftemp>xslt israel.xml israel.xsl <?xml version="1.0" encoding="utf-8"?><test>
<p dir="rtl"><span id="textStyle10">Some text.</span> </p> <p dir="rtl"><sup>1</sup></p> <p dir="rtl"><span id="textStyle10"><br/><br/></span></p> </test> T:\ftemp>type israel.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="p[span[normalize-space(.)='' and not(*)] and not(span[normalize-space() or *]) and not(*[not(self::span)])]"/>
<xsl:template match="span[not(*) and not(normalize-space(.))]"> </xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes--> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
-- XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19 XSLT/XQuery/XPath training: San Carlos, California 2010-04-26/30 Vote for your XML training: http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Remove element that all its d, Israel Viente | Thread | Re: [xsl] Remove element that all i, Israel Viente |
[xsl] Remove element that all its d, Israel Viente | Date | Re: [xsl] conditional increment und, anil an.kumar |
Month |