Re: [xsl] Matching only text nodes with certain (complicated) properties

Subject: Re: [xsl] Matching only text nodes with certain (complicated) properties
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 9 Jan 2009 10:08:29 -0800
> In XSLT 1.0 the problem is nastier and expensive, although doable. But when
> restricted to 1.0, one should keep in mind other options (such as judicious
> pre-processing) to alleviate problems either with performance or code
> complexity.
>
> It's doable because the pb can in fact be found on preceding:: (even when
> not on preceding-sibling::).

As the OP stated in his opening message,

"As far as I understand it, the main problem is that the expression I
have to construct for matching can't rely simply on the preceding:: or
ancestor:: axes since the <pb/> can really appear anywhere in the
tree."

The following XSLT 1.0 transformation has a template that correctly
matches (only) the required text nodes:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:strip-space elements="*"/>
	
	<xsl:template match="text()"/>
	
	<xsl:template match=
	 "text()
	   [count(preceding::text()[1]/preceding::node()
	          | preceding::text()[1]/ancestor::node()
	          )
	   &lt;
	    count((preceding::pb[1] | ancestor::pb[1])[last()]/preceding::node()
	          |
	           (preceding::pb[1] | ancestor::pb[1])[last()]/ancestor::node()
	         )
	    ]
	
	 "
	 >
	  ***Matching*** '<xsl:value-of select="."/>'

	</xsl:template>
</xsl:stylesheet>

When applied on this xml document:

<t>
	<item>
	before before
		<pb n="3"/>
 before a vowel.
		<pb n="26">
			<list>
				<item n="a">The
					<mentioned>thingy</mentioned>
				</item>
			</list>
		</pb>
	</item>
</t>

it correctly produces:

	  ***Matching*** '
 before a vowel.
		'

	
	  ***Matching*** 'The
					'



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play





On Fri, Jan 9, 2009 at 9:28 AM, Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> wrote:
> Hi,
>
> At 12:18 PM 1/9/2009, Martin wrote:
>>
>> Martin Honnen wrote:
>>>
>>> David B?rgin wrote:
>>>
>>>> In one template of my XSL stylesheet I'd like to match a text node with
>>>> certain properties. Namely, the template should match for any text node
>>>> where the nearest preceding non-empty text node is farther away than (=
>>>> comes before) the nearest preceding <pb/> (page-break milestone element).
>>>
>>>  <xsl:template
>>>   match="text()[preceding::text()[1] >> preceding::pb[1]]">
>>
>> I think you want << instead of >>:
>>  <xsl:template
>>   match="text()[preceding::text()[1] << preceding::pb[1]]">
>
> This will work in XSLT 2.0, although it sounds like you also want
>
> text()[preceding::text()[normalize-space()][1] << preceding::pb[1]]
>
> ... which makes sure that whitespace-only text nodes won't be considered in
> the test.
>
> In XSLT 1.0 the problem is nastier and expensive, although doable. But when
> restricted to 1.0, one should keep in mind other options (such as judicious
> pre-processing) to alleviate problems either with performance or code
> complexity.
>
> It's doable because the pb can in fact be found on preceding:: (even when
> not on preceding-sibling::).
>
> Cheers,
> Wendell
>
>
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>  Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================

Current Thread