tags:

views:

43

answers:

2

I have follwing problem in grouping the similer nodes with xsl:

input:

<?xml version="1.0" encoding="UTF-8"?>
<concept id="ads">
    <p>para1 content goes here</p>
    <Bullet>1</Bullet>
    <Bullet>2</Bullet>
    <Bullet>3</Bullet>
    <p>para2 content goes here</p>
    <Bullet>4</Bullet>
    <Bullet>5</Bullet>
    <p>para2 content goes here</p>
</concept>

Output should be:

<?xml version="1.0" encoding="UTF-8"?>
<concept id="ads">
    <p>para1 content goes here</p>
    <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    </ul>
    <p>para2 content goes here</p>
    <ul>
    <li>4</li>
    <li>5</li>
    </ul>
    <p>para2 content goes here</p>
</concept>

All the "Bullet" element which does not have imideate preceding sibling as "Bullet" should wrap up in "UL" and "Li" elements.

I am trying something like this but not able to achive the result:

<xsl:for-each select="Bullet[not(preceding-sibling::Bullet)]">
<ul>
<xsl:copy-of select="."/>
<xsl:variable name="current-name" select="generate-id(.)"/>
<xsl:for-each select="following-sibling::*[Bullet][generate-id(preceding-sibling::*[Bullet]) = $current-name]">
<xsl:copy-of select="."/>
</xsl:for-each>
</ul>
</xsl:for-each>

Please help.

---Related Question ----

This works fine on given xml input, but 1. In my actual xml these <Bullet> tags can appear anywhere under concept node, so grouping with <p> element does not work in that case.

  1. Currently I am processing all the nodes except "Bullet" nodes, so i need to match only <Bullet> nodes which is having immediate following sibling as <Bullet> and wrap the sequence in <ul> and each <Bullet> in <li>. My current context node is <concept>.

  2. Current stylesheet as follows:

    nodes which should render at the same place as in input.-->

Actual XML pattern:

<?xml version="1.0" encoding="UTF-8"?> 
<concept id="ads"> 
    <p>para1 content goes here</p> 
    <Body-text>Body1 content goes here</Body-text>
    <Bullet>1</Bullet> 
    <Bullet>2</Bullet> 
    <Bullet>3</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p> 
    <Bullet>4</Bullet> 
    <Bullet>5</Bullet> 
    <p>para3 content goes here</p>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
</concept>
<concept id="ads"> 
    <p>para1 content goes here</p> 
    <Body-text>Body1 content goes here</Body-text>
    <Bullet>1</Bullet> 
    <Bullet>2</Bullet> 
    <Bullet>3</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p> 
    <Bullet>4</Bullet> 
    <Bullet>5</Bullet> 
    <p>para3 content goes here</p>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
</concept>
+1  A: 

This stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()[1]|@*"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="Bullet[preceding-sibling::node()[1]
                               [not(self::Bullet)]]">
        <ul>
            <xsl:call-template name="makeLi"/>
        </ul>
        <xsl:apply-templates select="following-sibling::node()
                                                     [not(self::Bullet)][1]"/>
    </xsl:template>
    <xsl:template match="Bullet" name="makeLi">
        <li>
            <xsl:value-of select="."/>
        </li>
        <xsl:apply-templates select="following-sibling::node()[1]
                                                              [self::Bullet]"/>
    </xsl:template>
</xsl:stylesheet>

EDIT: Just changing the following applying "first Bullet" rule to first next not Bullet instead of p.

Output (wrapping your input with root element to be wellformed):

<concept id="ads">
    <p>para1 content goes here</p>
    <Body-text>Body1 content goes here</Body-text>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p>
    <ul>
        <li>4</li>
        <li>5</li>
    </ul>
    <p>para3 content goes here</p>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
</concept>
<concept id="ads">
    <p>para1 content goes here</p>
    <Body-text>Body1 content goes here</Body-text>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p>
    <ul>
        <li>4</li>
        <li>5</li>
    </ul>
    <p>para3 content goes here</p>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
</concept>

Note: Fine grained traversal.

With grouping, this stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="concept">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each-group select="*" 
                                group-adjacent="boolean(self::Bullet)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <ul>
                            <xsl:apply-templates select="current-group()"/>
                        </ul>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Bullet">
        <li>
            <xsl:value-of select="."/>
        </li>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

EDIT: Ussing group-adjacent.

Alejandro
please ignore my previous comment: I have edited my question, I would be thankful if you look at the updated problem and let me know your comment on that.
Pankaj
@Pankaj: Check my edit.
Alejandro
Thanks again Alejendro, You saved my week!
Pankaj
@Pankaj: You are wellcome
Alejandro
+1  A: 

This XSLT 2.0 transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
      <xsl:copy>
       <xsl:copy-of select="@*"/>
        <xsl:for-each-group select="*" group-starting-with="p">
          <xsl:copy-of select="current-group()[1]"/>
           <xsl:if test="current-group()[2]">
               <ul>
                <xsl:apply-templates select="current-group()[position() gt 1]"/>
              </ul>
          </xsl:if>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="Bullet">
      <li><xsl:value-of select="."/></li>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<concept id="ads">
    <p>para1 content goes here</p>
    <Bullet>1</Bullet>
    <Bullet>2</Bullet>
    <Bullet>3</Bullet>
    <p>para2 content goes here</p>
    <Bullet>4</Bullet>
    <Bullet>5</Bullet>
    <p>para2 content goes here</p>
</concept>

produces the wanted, correct result:

<concept id="ads">
   <p>para1 content goes here</p>
   <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
   </ul>
   <p>para2 content goes here</p>
   <ul>
      <li>4</li>
      <li>5</li>
   </ul>
   <p>para2 content goes here</p>
</concept>

Do note: The use of <xsl:for-each-group> with the group-starting-with attribute and the current-group() function.

Dimitre Novatchev
+1 I like this current group position test, it's a shorter expression so you don't have to put it in a variable for compacting the code. But, I must say that besides that is the same solution. The "genuine" comparison... it's to much, ja!
Alejandro
@Alejandro: By "genuine" I meant: "one that exploits as many XSLT 2.0 relevant to grouping features, as possible". And we both know what I mean: no keys, `<xsl:for-each-group>`, the `group-starting-with` attribute, `current-group()`
Dimitre Novatchev