tags:

views:

72

answers:

2

I cannot wrap <panel> tags to second level individual items as shown in Expected result bellow. Instead I get all 1.x element values into a single node with the xslt i have written bellow. Please help me.

Source xml

    <root>
    <step id="1">
        <content>
            <text>
                1.0 Sample first level step text
            </text>
        </content>
        <content/>
        <step>
            <content>
                <text>
                    1.1 Sample second level step text
                </text>
            </content>
        </step>
        <step>
            <content>
                <text>
                    1.2 Sample second level step text
                </text>
            </content>
        </step>
        <step>
            <content>
                <text>
                    1.3 Sample second level step text
                </text>
            </content>
        </step>
    </step>
    <step id="2">
        <content>
            <text>
                2.0 Sample first level step text
            </text>
        </content>
        <content/>
        <step>
            <content>
                <text>
                    2.1 Sample second level step text
                </text>
            </content>
        </step>
        <step>
            <content>
                <text>
                    2.2 Sample second level step text
                </text>
            </content>
        </step>
        <step>
            <content>
                <text>
                    2.3 Sample second level step text
                </text>
            </content>
        </step>
    </step>
</root>

Expected output

<panel>
    <panel>
        <panel>
            1.0 Sample first level step text
        </panel>
        <panel>
            1.1 Sample second level step text
        </panel>
        <panel>
            1.2 Sample second level step text
        </panel>
        <panel>
            1.3 Sample second level step text
        </panel>
    </panel>
    <panel>
        <panel>
            2.0 Sample first level step text
        </panel>
        <panel>
            2.1 Sample second level step text
        </panel>
        <panel>
            2.2 Sample second level step text
        </panel>
        <panel>
            2.3 Sample second level step text
        </panel>
    </panel>
</panel>

My XSLT

<xsl:template match="/">
    <panel>
        <xsl:apply-templates/>
    </panel>
</xsl:template>

<xsl:template match="root/step" >
    <panel>
        <panel>
            <xsl:apply-templates select ="content/text/node()"></xsl:apply-templates>
        </panel>
        <panel>
            <xsl:apply-templates select ="step/content/text/node()"></xsl:apply-templates>
        </panel>
    </panel>
</xsl:template>
A: 

The trick is usage // accessor

<xsl:template match="/">
    <panel>
        <xsl:apply-templates select='//text'/>
    </panel>
</xsl:template>

<xsl:template match="text" >
        <panel>
            <xsl:apply-templates select ="./node()"></xsl:apply-templates>
        </panel>
</xsl:template>

place expected formatting instead of my node()

EDITED To handle grouping as you ask, add one more

<xsl:template match="/">
    <panel>
        <xsl:apply-templates select='/root/step' mode="root"/>
    </panel>
</xsl:template>
<xsl:template match="step" mode="root"><!-- mode allows distinguish another tag 'step'-->
     <panel> <!-- now we can convert tree to planar -->
         <xsl:apply-templates select='//text'/>
     </panel>    
</xsl:template>

<xsl:template match="text" >
        <panel>
            <!-- format text from node text there -->
            <xsl:apply-templates select ="text()"></xsl:apply-templates>
        </panel>
</xsl:template>
Dewfy
Thank you all for the response. One more thing, maybe I don’t get this XSLT at all!. How can I add another <panel> grouping at <step id=1> level. Please refer initial Source XML and Expected output in my post.
m00sila
This is *terrible* XSLT code. I approve of whoever downvoted this answer. Look at the solution of @Alejandro. It is really clean and elegant.
Dimitre Novatchev
Ah, I see, we're not flattening the tree all the way. (Deleted prev comment.)
Owen S.
+3  A: 

This should work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 

 <xsl:template match="root|root/step|text"> 
   <panel> 
    <xsl:apply-templates/> 
   </panel> 
 </xsl:template> 

</xsl:stylesheet> 

Edit: If you want to beautify the things a bit, add this template:

 <xsl:template match="text()"> 
   <xsl:value-of select="normalize-space()"/> 
 </xsl:template> 

Edit 2: I've change the pattern according new input and output document. This is in case of any other wound may need it.

Alejandro
Thank you all for the response. One more thing, maybe I don’t get this XSLT at all!. How can I add another <panel> grouping at <step id=1> level. Please refer initial Source XML and Expected output in my post. –
m00sila
I think i got it. Thanks. <xsl:template match="root|root/step|text"> <panel> <xsl:apply-templates/> </panel> </xsl:template>
m00sila