Re: [xsl] small grouping task

Subject: Re: [xsl] small grouping task
From: Fred Christian <fredc@xxxxxxxxxx>
Date: Thu, 26 Feb 2009 12:24:11 -0800
Thanks Ken for the help.
Now I know what specs I need to add. Your second xsl example comes close, you almost read my mind. I will try to add the rest of the requirements succinctly.
I am using Saxon and a xsl 2.0 solution is great.
> 2), the other making the assumption that each list item is all of the content (except <br/>) that follows each <input/>
Correct. But the first <br/> Not more.


Another valid assumption is, After the <br/>

Other things to make work are.
-a- There are random text and tags between the '<p class="problem">' and the first <input>...<br/> pair.
-b- There are text and tags after that last <input>... <br/> pair and the closing </p>
Those need to be preserved.


In your second xsl, the "-a-" items were removed from the output. And the "-b-" items were wrapped with the last </li> and </ul>. The last </li></ul> needs to end sooner.

Thanks for getting me close. I will see if I can figure it out, and if you are able to try again, that would be awesome.

Note my new source a few more tags
Maybe the <p class="problem"> is just a distraction?? But the <input>'s will always be children of the <p class="problem">
--------------------------------------------------------------------
<r>
<p>text</p>
<randomtag>surronding xhtml</randomtag>
<p class="problem">some <b>xhtml</b> with<br/> no inputs<br/>
<input type="radio" /> <span class="p">a</span><b>d </b><br />
<input type="radio" /> <b>g </b><span class="p">b</span><br />
<p>more</p> generic <b>xhtml</b> with<br/> no inputs<img src="l"/>
</p>
<b>a</b>surronding xhtml<b>b</b>
</r>


---------------------------------------------

desired output. Only the <input>.....<br/> pairs are altered
--------------------------------------------------------------------
<r>
   <p>text</p>
   <randomtag>surronding xhtml</randomtag>
<p class="problem">some <b>xhtml</b> with<br/> no inputs<br/>
 <ul class="m">
  <li class="m"> <span class="p">a</span><b>d </b></li>
  <li class="m"> <b>g </b><span class="p">b</span></li>
 </ul>
   <p>more</p> generic <b>xhtml</b> with<br/> no inputs<img src="l"/>
</p>
<b>a</b>surronding xhtml<b>b</b>
</r>
------------------------------------------------------------------------------


current incorrect output, note: has missing items and misplaced items. ---------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<r>
   <p>text</p>
   <randomtag>surronding xhtml</randomtag>
  <ul class="m">
     <li class="m">
        <span class="p">a</span>
        <b>d </b>
     </li>
     <li class="m">
        <b>g </b>
        <span class="p">b</span>
        <p>more</p> generic <b>xhtml</b> with no inputs<img src="l"/>
     </li>
  </ul>
  <b>a</b>surronding xhtml<b>b</b>
</r>


---------------------------------------------------------


current stylesheet (same as Ken's)
----------------------------------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="p[@class = 'problem' and input]">
 <!--start the list-->
 <ul class="m">
   <!--find the members of the list, assuming multiple items per member-->
   <xsl:for-each-group select="node()" group-starting-with="input">
     <xsl:if test="self::input"><!--found a group with an input-->
       <li class="m">
         <!--copy everything except <br/> elements-->
         <xsl:apply-templates select="current-group()[position()>1]
                                                     [not(self::br)]"/>
       </li>
     </xsl:if>
   </xsl:for-each-group>
 </ul>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Current Thread