Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge adjacent sibling nodes with XSLT

Tags:

xslt

I have a question that caused me a terrible headache. Please help me. The input is:

<body>
 <p class="section"> section 1 </p>
 <p class="code"> some code </p>
 <p class="code"> following code </p>
 <p class="code"> following code </p>
 <p class="section"> section 2 </p>
 <p class="code"> other code </p>
 <p class="code"> following code </p>
 <p class="code"> following code </p>
 <p class="section"> section 3 </p>
 <p class="code"> still other code </p>
 <p class="code"> following </p>
 <p class="code"> following </p>
</body>

The output I'd like:

<body>
 <p class="section"> section 1 </p>
 <pre> some code following code following code </pre>
 <p class="section"> section 2 </p>
 <pre> other code following code following code </pre>
 <p class="section"> section 3 </p>
 <pre> still other code following following </pre>
</body>

The problem is to collapse to a <pre> tag all adjacent <p class="code"> tags. Don't find a way to do this using XSLT. Do you think a solution exists?

like image 994
Emiliano Poggi Avatar asked Jan 22 '23 17:01

Emiliano Poggi


1 Answers

You don't need to rebuild your XML, take a look here: XSLT Grouping Siblings.

like image 150
Rubens Farias Avatar answered Feb 01 '23 06:02

Rubens Farias