[xsl] unparsed-text and for-each-group

Subject: [xsl] unparsed-text and for-each-group
From: James Cummings <cummings.james@xxxxxxxxx>
Date: Wed, 21 Dec 2005 17:22:03 +0000
Hiya,

For the fun of it I wanted to start playing around more with
unparsed-text() to read in some poems in plain text and convert them
to XML.  What I want to do is wrap the whole file in an element (let's
say called 'poem') and each line of poetry in a line element ('l'). 
Moreover, if there is a blank line (either two newlines or
newline-whitespace-newline) I want to group these stanzas into
linegroups ('lg').  The first line will always (let's say) be a title,
if the second line isn't blank/whitespace then it is an author.

So for example, if we have this well known poem:
------
The Tiger
William Blake

TIGER, tiger, burning bright	
In the forests of the night,	
What immortal hand or eye	
Could frame thy fearful symmetry?	

In what distant deeps or skies
Burnt the fire of thine eyes?	
On what wings dare he aspire?	
What the hand dare seize the fire?	

And what shoulder and what art	
Could twist the sinews of thy heart?
And when thy heart began to beat,	
What dread hand and what dread feet?	

What the hammer? what the chain?	
In what furnace was thy brain?	
What the anvil? What dread grasp
Dare its deadly terrors clasp?	

When the stars threw down their spears,	
And water'd heaven with their tears,	
Did He smile His work to see?	
Did He who made the lamb make thee?

Tiger, tiger, burning bright	
In the forests of the night,	
What immortal hand or eye	
Dare frame thy fearful symmetry?	
------

What I want to automagically get from this is:

------
<?xml version="1.0"?>
<poem>
<title>The Tiger</title>
<author>William Blake</author>
<lg>
<l>TIGER, tiger, burning bright</l>	
<l>In the forests of the night,	 </l>
<l>What immortal hand or eye	 </l>
<l>Could frame thy fearful symmetry?</l>	
</lg>
<lg>
<l>In what distant deeps or skies</l>
<l>Burnt the fire of thine eyes?	             </l>
<l>On what wings dare he aspire?	 </l>
<l>What the hand dare seize the fire?	 </l>
</lg>
<lg>
<l>And what shoulder and what art	 </l>
<l>Could twist the sinews of thy heart?</l>
<l>And when thy heart began to beat,	 </l>
<l>What dread hand and what dread feet?</l>	
</lg>
<lg>
<l>What the hammer? what the chain?	 </l>
<l>In what furnace was thy brain?	 </l>
<l>What the anvil? What dread grasp</l>
<l>Dare its deadly terrors clasp?	 </l>
</lg>
<lg>
<l>When the stars threw down their spears,</l>	
<l>And water'd heaven with their tears,	 </l>
<l>Did He smile His work to see?	 </l>
<l>Did He who made the lamb make thee?</l>
</lg>
<lg>
<l>Tiger, tiger, burning bright	 </l>
<l>In the forests of the night,	 </l>
<l>What immortal hand or eye	 </l>
<l>Dare frame thy fearful symmetry?</l>	
</lg>
</poem>
------

Though, of course, it might be good to remove any blank whitespace at
the ends of lines since I'm not intending to test this on any e e
cummings. ;-)

Am I right in assuming that the best way to do this is two nested
xsl:for-each-group's using unparsed-text()?  I'm a bit confused on how
to do the <lg> elements....

Suggestions? (Other than that I should be going to the office Xmas
party instead of doing this...)

-James

--
James Cummings, Cummings dot James at GMail dot com

Current Thread