How to populate the span tags for all text nodes between specific self closing tags.

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-04-21T18:42:17Z Indexed on 2010/04/21 23:03 UTC
Read the original article Hit count: 276

Filed under:
|
|

I want to populate span tags for all text nodes between the self closing tags s1 and s2 having the same id.

Eg. For the input:

<a>
  <b>Some <s1 id="1" />text here</b>
  <c>Some <s2 id="1"/>more text <s1 id="2"/> here<c/>
  <d>More data</d>
  <e>Some <s2 id="2" />more data</e>
</a>

In the above input i want to enclose every text node between <s1 id="1"/> and <s2 id="1" /> with span tags. Also all the text node between <s1 id="2" /> and <s2 id="2" />

Expected Output:

<a>
  <b>Some <span class="spanClass" id="1a">text here</span></b>
  <c><span class="spanClass" id="1b">Some </span>more text <span class="spanClass" id="2a"> here</span></c>
  <d><span class="spanClass" id="2b">More data</span></d>
  <e><span class="spanClass" id="2c">Some </span>more data</e>
</a>

I am not concened about the pattern of the id tag populated for the span as along it is unique. If the transformation of the input to the output form requires the list of ids of the s1 , s2 tag pairs say 1, 2 etc i can assume that it in place in any form if required.

Hope i am clear. How can this be achieved using XSL?

EDIT: The input can have any structure and not exactly the same as seen in the sample input. It can have any number of s1, s2 tag pairs but each pair will have a unique id.Adding another sample input and output pattern for more information.

Input:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>This is my title</title>
</head>
<body>
  <h1 align="center">This <s1 id="1" />is my <s2 id="1" />heading</h1>
  <p>
    Sample content <s1 id="2" />Some text here. Some content here.
  </p>
  <p>
    Here you <s2 id="2" />go.
  </p>
</body>
</html>

Desired output:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>This is my title</title>
</head>
<body>
  <h1 align="center">This <span id="1">is my </span>heading</h1>
  <p>
    Sample content <span id="2">Some text here. Some content here.</span>
  </p>
  <p>
    <span id="2">Here you </span>go.
  </p>
</body>
</html>

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xslt-2.0