Adding <span> tags to all text nodes between custom self closing tags.
- by Rachel
I have a pair of custom self closing tags s1 and s2 defined in namespace x in my xhtml. For each tag pair s1, s2 having the same id, I want to add span tags to all the text nodes between them. Each s1, s2 tag pair have a unique id. The s1 tag has an attribute 'styleName' which needs to be copied as the class name for the span tags populated for the s1,s2 pair. Within a s1, s2 tag pair, other s1, s2 tags can occur. It is the id attribute of the tags s1 and s2 that help us to find the postion from where we need to start populating the span(for text nodes alone) and the end where we need to stop. In case of common text nodes that is part of the multiple s1, s2 pairs then the span tags needs to be opened and closed appropirately as shown in the sample below. I am not specific with the format of the id populated for the span tag. Along as it is unique it is fine. Can we achive this kind of a solution using XSL. I am using Saxon processor.
Sample input:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://sample.org">
<head>
<title>This is my title</title>
</head>
<body>
<h1 align="center">This <x:s1 id="1" styleName="name_1"/>is my <x:s2 id="1" />heading</h1>
<p>
Sample content <x:s1 id="2" styleName="name_2"/> Some text here.
</p>
<p>
Here you <x:s2 id="2" />go.
</p>
<p>
<x:s1 id="3" styleName="name_3"/>This <x:s1 id="4" styleName="name_4"/>is just a simple text <x:s2 id="4" />Some text here.<x:s2 id="3" /> Some content here.
</p>
<p>
Use this <x:s1 id="5" styleName="name_5"/>space.
</p>
<p>
Indroducing <x:s1 id="6" styleName="name_6"/> more information.
</p>
<p>
Can add some <x:s2 id="6" />more content here.
</p>
<p>
Sample content <x:s2 id="5" />Some text here. Some content here.
</p>
<p>
<x:s1 id="7" styleName="name_7"/>This is a complex data. <x:s1 id="8" styleName="name_8"/>Framing a long sentence to <x:s2 id="7" />accomodate all possible <x:s2 id="8" />scenarios.
</p>
<p>
<x:s1 id="9" styleName="name_9"/>More data can be <x:s1 id="10" styleName="name_10"/>added here.
</p>
<p>
Trying to include here.
</p>
<p>
Modifying <x:s2 id="9" />content <x:s2 id="10" />here.
</p>
</body>
</html>
Sample output:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://sample.org">
<head>
<title>This is my title</title>
</head>
<body>
<h1 align="center">This <span id="1_1" class="name_1">is my </span>heading</h1>
<p>
Sample content <span id="2_1" class="name_2"> Some text here.</span>
</p>
<p>
<span id="2_2" class="name_2">Here you </span>go.
</p>
<p>
<span id="3_1" class="name_3">This <span id="4_1" class="name_4">is just a simple text </span>Some text here.</span> Some content here.
</p>
<p>
Use this <span id="5_1" class="name_5">space.</span>
</p>
<p>
<span id="5_2" class="name_5">Indroducing <span id="6_1" class="name_6"> more information.</span></span>
</p>
<p>
<span id="5_3" class="name_5"><span id="6_2" class="name_6">Can add some </span>more content here.</span>
</p>
<p>
<span id="5_4" class="name_5">Sample content </span>Some text here. Some content here.
</p>
<p>
<span id="7_1" class="name_7">This is a complex data.</span> <span id="8_1" class="name_8"><span id="7_2" class="name_7">Framing a long sentence to </span></span><span id="8_2" class="name_8">accomodate all possible </span>scenarios.
</p>
<p>
<span id="9_1" class="name_9">More data can be <span><span id="10_1" class="name_10"><span id="9_2" class="name_9">added here.</span></span>
</p>
<p>
<span id=10_2 class="name_10"><span id="9_3" class="name_9">Trying to include here.</span></span>
</p>
<p>
<span id=10_3 class="name_10"><span id="9_4" class="name_9">Modifying</span></span><span id="10_4" class="name_10">content </span>here.
</p>
</body>
</html>
Thanks.