xslt 2.0 - XSL grouping by group-starting-with -


i have problem xsl. have xml documents (similar docbook) have transformed xsl. of documents contains sect2-title, , next sibling part of tags. starting xml has following structure:

    <?xml version="1.0" encoding="utf-8"?>     <book>       <part>         <article role="content" title="contenidos">           <sect1 title="1. concepto y características de juego">             <para>el juego es una ...</para>             <sect2-title>1.1. el modelo lúdico:</sect2-title>             <para>sin embargo, sí existe un hecho ...</para>             <sidebar>               <sidebar-role>vocabulario</sidebar-role>               <sidebar-para>de ahí el origen de ....</sidebar-para>             </sidebar>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />               <caption>fig. 1.2. ....</caption>             </imageobject>             <para>en el juego social....</para>             <sect2-title>1.2. características ...</sect2-title>             <para>dadas ...</para>             <itemizedlist>               <listitem>                 <para>• el juego es...</para>               </listitem>               <listitem>                 <para>• es placentera...</para>               </listitem>             </itemizedlist>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />               <caption>fig. 1.3. ...</caption>             </imageobject>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />               <caption>tabla 1.1. ...</caption>             </imageobject>           </sect1>          </article>       </part>     </book> 

my desired result be:

    <?xml version="1.0" encoding="utf-8"?>     <book>       <part>         <article role="content" title="contenidos">           <sect1 title="1. concepto y características de juego">             <para>el juego es una ...</para>             <sect2 title="1.1. el modelo lúdico:">               <para>sin embargo, sí existe un hecho ...</para>               <sidebar>                 <sidebar-role>vocabulario</sidebar-role>                 <sidebar-para>de ahí el origen de ....</sidebar-para>               </sidebar>               <imageobject>                 <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />                 <caption>fig. 1.2. ....</caption>               </imageobject>               <para>en el juego social....</para>             </sect2>             <sect2 title="1.2. características ...">               <para>dadas ...</para>               <itemizedlist>                 <listitem>                   <para>• el juego es...</para>                 </listitem>                 <listitem>                   <para>• es placentera...</para>                 </listitem>               </itemizedlist>               <imageobject>                 <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />                 <caption>fig. 1.3. ...</caption>               </imageobject>               <imageobject>                 <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />                 <caption>tabla 1.1. ...</caption>               </imageobject>             </sect2>           </sect1>         </article>       </part>     </book> 

the xsl macht is:

    <xsl:template match="sect1">         <xsl:copy>             <xsl:copy-of select="@*"/>             <xsl:for-each-group select="*[preceding-sibling::sect2-title]"                                 group-starting-with="sect2-title">                 <sect2>                     <xsl:copy-of select="current-group()"/>                 </sect2>             </xsl:for-each-group>         </xsl:copy>     </xsl:template> 

someone can me. tried using xsl 2.0 can not proper solution.

here stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"   xmlns:xs="http://www.w3.org/2001/xmlschema"   xmlns:mf="http://example.org/mf"   exclude-result-prefixes="xs mf">  <xsl:output indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="@* | node()">   <xsl:copy>     <xsl:apply-templates select="@* , node()"/>   </xsl:copy> </xsl:template>  <xsl:template match="sect1">   <xsl:copy>     <xsl:apply-templates select="@*"/>     <xsl:for-each-group select="*" group-starting-with="sect2-title">       <xsl:choose>         <xsl:when test="self::sect2-title">           <sect2 title="{.}">             <xsl:apply-templates select="current-group() except ."/>           </sect2>         </xsl:when>         <xsl:otherwise>           <xsl:apply-templates select="current-group()"/>         </xsl:otherwise>       </xsl:choose>     </xsl:for-each-group>   </xsl:copy> </xsl:template>  </xsl:stylesheet> 

it transforms input

<?xml version="1.0" encoding="utf-8"?>     <book>       <part>         <article role="content" title="contenidos">           <sect1 title="1. concepto y características de juego">             <para>el juego es una ...</para>             <sect2-title>1.1. el modelo lúdico:</sect2-title>             <para>sin embargo, sí existe un hecho ...</para>             <sidebar>               <sidebar-role>vocabulario</sidebar-role>               <sidebar-para>de ahí el origen de ....</sidebar-para>             </sidebar>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />               <caption>fig. 1.2. ....</caption>             </imageobject>             <para>en el juego social....</para>             <sect2-title>1.2. características ...</sect2-title>             <para>dadas ...</para>             <itemizedlist>               <listitem>                 <para>• el juego es...</para>               </listitem>               <listitem>                 <para>• es placentera...</para>               </listitem>             </itemizedlist>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />               <caption>fig. 1.3. ...</caption>             </imageobject>             <imageobject>               <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />               <caption>tabla 1.1. ...</caption>             </imageobject>           </sect1>          </article>       </part>     </book> 

into result

<?xml version="1.0" encoding="utf-8"?> <book>    <part>       <article role="content" title="contenidos">          <sect1 title="1. concepto y características de juego">             <para>el juego es una ...</para>             <sect2 title="1.1. el modelo lúdico:">                <para>sin embargo, sí existe un hecho ...</para>                <sidebar>                   <sidebar-role>vocabulario</sidebar-role>                   <sidebar-para>de ahí el origen de ....</sidebar-para>                </sidebar>                <imageobject>                   <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg"/>                   <caption>fig. 1.2. ....</caption>                </imageobject>                <para>en el juego social....</para>             </sect2>             <sect2 title="1.2. características ...">                <para>dadas ...</para>                <itemizedlist>                   <listitem>                      <para>• el juego es...</para>                   </listitem>                   <listitem>                      <para>• es placentera...</para>                   </listitem>                </itemizedlist>                <imageobject>                   <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg"/>                   <caption>fig. 1.3. ...</caption>                </imageobject>                <imageobject>                   <imagedata fileref="../../media/img/ud01_7151_img01.jpeg"/>                   <caption>tabla 1.1. ...</caption>                </imageobject>             </sect2>          </sect1>       </article>    </part> </book> 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -