Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping elements on the same level

Tags:

xml

xslt

I'm having an issue with my transform and was hoping for some ideas, I'm dealing with a really flat input document where all important nodes are siblings of one another.

It kind of looks like this:

<title1> Rule 51 </title1>
<p> text here </p>
<p> text here </p>
<note> Source </note>
<p> text here </p>
<title1> Rule 52 </title1>
<p> text here </p>
<p> text here </p>
<note> Source </note>
<p> text here </p>

My goal is for this input to look like this:

  <section>
       <title1> Rule 51 </title1>
       <p> text here </p>
       <p> text here </p>
       <note> Source </note>
           <p> text here </p>
   </section>

   <section>
       <title1> Rule 52 </title1>
       <p> text here </p>
       <p> text here </p>
       <p> text here </p>
       <p> text here </p>
       <note> Source </note>
           <p> text here </p>
   </section>

As you can see above my main goal is grouping each title1 and all of it's following siblings until it hits another title1 into a section element. Any ideas??

Thanks in advance.

like image 719
Avbasot Avatar asked Apr 18 '26 18:04

Avbasot


1 Answers

Try

<!-- Change the match pattern to match the parent of the input you showed. -->
<xsl:template match="blockquote">
  <xsl:for-each-group group-starting-with="h:h4" select="*">
    <section>
      <xsl:copy-of select="current-group()" />

in XSLT 2.0.

If you can only use XSLT 1.0, look up Muenchian Grouping, and holler if you need help.

like image 164
LarsH Avatar answered Apr 21 '26 21:04

LarsH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!