I'm hoping to get some advice on how to group adjacent elements with the same name in an unordered list using XSLT 1.0.
Here's some example XML:
<Article>
<TextContent>
<p>lorem</p>
<VisualContent id="1" />
<VisualContent id="2" />
<VisualContent id="3" />
<p>ipsum</p>
<VisualContent id="4" />
<p>dolor</p>
<VisualContent id="5" />
</TextContent>
</Article>
Here's the output I'd like:
<Article>
<HtmlContent>
<p>lorem</p>
<ul>
<li><img data-id="1" /></li>
<li><img data-id="2" /></li>
<li><img data-id="3" /></li>
</ul>
<p>ipsum</p>
<img data-id="4" />
<p>dolor</p>
<img data-id="5" />
</HtmlContent>
</Article>
Unfortunately, XSLT 1.0 is a strict requirement for this one. Any suggestions would be greatly appreciated.
I am also new to XSLT.
This is not your expected output, I could not get the
<ul>
, but see if you can build on top of this. I will also try.This is the output:
After finding out, that I need a current version of xsltproc (1.1.29), I further changed the style sheet. Grouping worked only for children of TextContent, but I needed grouping to work on any nesting level.
Here is my input:
I use the following style sheet:
Now, grouping works on any element level. :-)
I have skipped the part where
<TextContent>
turns into<HtmlContent>
and<VisualContent id="n" />
becomes<img data-id="n" />
because the question is difficult enough without these distractions.The method I have chosen looks at the first preceding sibling whose name is not the same as the current element's name. The unique ID of that sibling is the key by which the adjacent elements of same name can be grouped:
When applied to your input example, the following result is produced:
EDIT: Here's a modified version that only groups adjacent "VisualContent" elements:
This could probably use some streamlining, but I think the principle is clear and I need to get some sleep...