1

I had a problem in xsl:for-each-group and it was very nicely solved in here. Now I have some other problem. I have like this as input.

<?xml version="1.0" encoding="UTF-8"?>

    <body>
       <p name ="section">this is section</p>
       <p name="h-title" other="main">Introduction</p>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title" other=" other-h2">XSLT</p>
       <p name="">
          <p1 name="bold"> XSLT is used to write stylesheets.</p1>
       </p>
       <p name="h2-title " name="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name ="section">this is section</p>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title " other=" other-h2">XSLT</p>
       <p name ="section">this is section</section>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title " other=" other-h2">XSLT</p>
    </body>

Now my wanted output is this

<?xml version="1.0" encoding="UTF-8"?>
<body>
   <p name="h-title " other="main">Introduction</p>
   <section>
   <p name ="section">this is section</p>
   <h1>
      <p name="h1-title " other="other-h1"> XSLT and XQuery </p>
      <h2>
         <p name="h2-title " other="other-h2">XSLT</p>
         <p name="">
            <p1 name="bold">XSLT is used to write stylesheets.
            </p1>
         </p>
      </h2>
      <h2>
         <p name="h2-title " other="other-h2"> XQuery is used to query XMLdatabases    
         </p>
         <p name="">
            <p name="bold"> XQuery is used to query XML databases.</p>
         </p>
         <h3>
            <p name="h3-title " name="other-h3">XQuery and stylesheets</p>
            <p name="">
            <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        </h3>
      </h2>
</h1>
</section>
<section>
<p name ="section">this is section</p>
<h1>
            <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <h2>   
            <p name="h2"-title other=" other-h2">XSLT</p>
       </h2>
</h1>
</section>
<section>
<p name ="section">this is section</p>
<h1>
            <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <h2>   
            <p name="h2"-title other=" other-h2">XSLT</p>
       </h2>
</h1>
</section>
</body>

My used stylesheet(not working properly)

    <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.com/mf"
      exclude-result-prefixes="xs mf">

    <xsl:param name="prefix" as="xs:string" select="'h'"/>
    <xsl:param name="suffix" as="xs:string" select="'-title'"/>

    <xsl:output method="html" version="4.0" indent="yes"/>

    <xsl:function name="mf:group" as="node()*">
      <xsl:param name="items" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix,$level, $suffix)]">
        <xsl:choose>
          <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="h{$level}">
              <xsl:apply-templates select="."/>
              <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:function>

    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="body" name ="myTemplate">
      <xsl:copy>
        <xsl:sequence select="mf:group(*, 1)"/>
      </xsl:copy>
    </xsl:template>

   <xsl:template match="body[@name='section']">
        <section>
            <xsl:call-template name="myTemplate"/>
        </section>
    </xsl:template>    
    </xsl:stylesheet>

But this is not correct. Things like <p name ="section">this is section</p> appear widely. not only that, there are few others like this. If someone please show me how to handle with section I will be able to do that for others too. Please tell me how to do this correctly.

ADDED

<body>
<intro>
<p></p>
<p></p>
</intro>

<section>
<para>
</para>
<h1></h2>
<h2></h2>
</section>

<section>
<para>
</para>
<h1></h2>
<h2></h2>
</section>

<sumary>
</summary>
</body>

what I did

<xsl:for-each-group select="*" group-starting-with="p[@name = 'intro']">
            <intro>
                    <xsl:apply-templates select="current()"/>
            </intro>
</xsl:for-each-group>
Community
  • 1
  • 1
Setinger
  • 169
  • 3
  • 11
  • Setinger: I really couldn't understand what the transformation is supposed to do. Please, edit the question and explain. Probably a very small example will suffice -- and the requirement should be only the one that you cannot implement -- please, skip all other requirements. A simple example XML document could probably be just 10 lines long. – Dimitre Novatchev Jul 24 '12 at 02:51
  • @Dimitre, I added more to the question with simple levels. Please see that. – Setinger Jul 26 '12 at 05:32
  • Setinger, you have added a sample, please explain what that is, is that a possible input you have, or some output you want? And if it is a possible input, you will need to show which output you want for that as well. – Martin Honnen Jul 26 '12 at 09:41
  • No that's the output structure. ^_^ input is like i told before.(at first) – Setinger Jul 26 '12 at 10:24
  • @Setinger: Sorry, but this addition even makes the question more unclear. What the title asks is "Apply a template between two specific nodes in XSLT". I don't see any such question asked at all. I expect a question with such title to show us a simple XML document (up to a dozen lines) to specify two nodes in it and to ask how a specific template should be applied on all nodes "between" these two specified nodes -- no such requirement is present in your question. Giving up ... – Dimitre Novatchev Jul 26 '12 at 11:58
  • @DimitreNovatchev please see new question. – Setinger Jul 27 '12 at 18:17

1 Answers1

2

I think you mainly want another grouping step; the 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.com/mf"
  exclude-result-prefixes="xs mf">

<xsl:param name="prefix" as="xs:string" select="'h'"/>
<xsl:param name="suffix" as="xs:string" select="'-title'"/>

<xsl:output method="html" version="4.0" indent="yes"/>

<xsl:function name="mf:group" as="node()*">
  <xsl:param name="items" as="node()*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix, $level, $suffix)]">
    <xsl:choose>
      <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
        <xsl:apply-templates select="current-group()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="h{$level}">
          <xsl:apply-templates select="."/>
          <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:function>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="body">
  <xsl:copy>
    <xsl:for-each-group select="*" group-starting-with="p[@name = 'section']">
      <section>
        <xsl:sequence select="mf:group(current-group(), 1)"/>
      </section>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

transforms the corrected input

<?xml version="1.0" encoding="UTF-8"?>

    <body>
       <p name ="section">this is section</p>
       <p name="h-title" other="main">Introduction</p>
       <p name="h1-title" other="other-h1">XSLT and XQuery</p>
       <p name="h2-title" other=" other-h2">XSLT</p>
       <p name="">
          <p1 name="bold"> XSLT is used to write stylesheets.</p1>
       </p>
       <p name="h2-title" other="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name ="section">this is section</p>
       <p name="h1-title" other="other-h1">XSLT and XQuery</p>
       <p name="h2-title" other=" other-h2">XSLT</p>
       <p name ="section">this is section</p>
       <p name="h1-title" other="other-h1">XSLT and XQuery</p>
       <p name="h2-title" other=" other-h2">XSLT</p>
    </body>

into the result

<body>
   <section>
      <p name="section">this is section</p>
      <p name="h-title" other="main">Introduction</p>
      <h1>
         <p name="h1-title" other="other-h1">XSLT and XQuery</p>
         <h2>
            <p name="h2-title" other=" other-h2">XSLT</p>
            <p name="">

               <p1 name="bold"> XSLT is used to write stylesheets.</p1>

            </p>
         </h2>
         <h2>
            <p name="h2-title" other="other-h2">XQuery</p>
            <p name="">

               <p1 name="bold"> XQuery is used to query XML databases.</p1>

            </p>
            <h3>
               <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
               <p name="">

                  <p1 name="bold"> XQuery is used to query XML databases.</p1>

               </p>
            </h3>
         </h2>
      </h1>
   </section>
   <section>
      <p name="section">this is section</p>
      <h1>
         <p name="h1-title" other="other-h1">XSLT and XQuery</p>
         <h2>
            <p name="h2-title" other=" other-h2">XSLT</p>
         </h2>
      </h1>
   </section>
   <section>
      <p name="section">this is section</p>
      <h1>
         <p name="h1-title" other="other-h1">XSLT and XQuery</p>
         <h2>
            <p name="h2-title" other=" other-h2">XSLT</p>
         </h2>
      </h1>
   </section>
</body>

That has the structure you posted I think, with the exception of the <p name="h-title" other="main">Introduction</p> element being inside a section while your posted example moved it to the top. I am not sure what are the rules for doing that so I have not tried to implement that. Please clarify whether you simply want to move that single element to the top and not apply the grouping to it or whether there are more complex rules to exempt certain elements.

[edit]In case you simply want to move all p[@name = 't-title'] to the top and not group them then the following adaption of above stylesheet should do the job:

<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.com/mf"
  exclude-result-prefixes="xs mf">

<xsl:param name="prefix" as="xs:string" select="'h'"/>
<xsl:param name="suffix" as="xs:string" select="'-title'"/>

<xsl:output method="html" version="4.0" indent="yes"/>

<xsl:function name="mf:group" as="node()*">
  <xsl:param name="items" as="node()*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix, $level, $suffix)]">
    <xsl:choose>
      <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
        <xsl:apply-templates select="current-group()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="h{$level}">
          <xsl:apply-templates select="."/>
          <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:function>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="body">
  <xsl:copy>
    <xsl:apply-templates select="p[@name = 'h-title']"/>
    <xsl:for-each-group select="* except p[@name = 'h-title']" group-starting-with="p[@name = 'section']">
      <section>
        <xsl:sequence select="mf:group(current-group(), 1)"/>
      </section>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you very much Martin. This is really great. This works great with the example I gave. But like I told you when I have more levels it is difficult. I added example levels to the question and what I tried to make that working too. but it is not correct. can you tell what I should do to that. – Setinger Jul 26 '12 at 05:26
  • please see my new question if I am confusing here. – Setinger Jul 27 '12 at 18:18