xsl-list
[Top] [All Lists]

RE: [xsl] another placement of nodes question

2010-01-25 09:36:05
Hi,

It's not clear to me what should happened  if a <spec> element occurs
after the <desc> on level1.

But I don't think you need to group here...

Why not just iterate over all level level2/item and check when
position() = last() and then output the <spec>?

Robby Pelssers



-----Original Message-----
From: a kusa [mailto:akusa8(_at_)gmail(_dot_)com] 
Sent: Monday, January 25, 2010 3:53 PM
To: xsl-list
Subject: [xsl] another placement of nodes question

Hi

I have a scenario as shown below in my xml:

Source XML:

<root>
<level1>
<st1>
<desc><text>sample desc</text></desc>
<!-- A <spec> element can occur here as well-->
<level2>
<item><text>r1 </text></item>
<item><text>r2</text></item>
</level2>
<spec><para>Some spec 1</para></spec>
<spec><para>Some spec 2</para></spec>
</st1>
</level1>
</root>

Here is my desired output XML:

<root>

<step1>
<text>sample desc</text>

<step2>
<text>r1</text>
</step2>

<step2>
<text>r2</text>
<spec><para>Some spec 1</para></spec>
<spec><para>Some spec 2</para></spec>

</step2>

</step1>
</root>

Please note that every item under level2 becomes a step2 in my output
XML.


Here is my XSL for level2:

<xsl:template match="level2">

<xsl:for-each-group select="./item" group-by=".">
<text><xsl:apply-templates/></text>
<xs:apply-templates
select="../parent::st1/child::spec[not(preceding-sibling::*[1][self::des
c])]
</xsl:for-each-group>
</xsl:template>


The reason I write the condition
"not(preceding-sibling::*[1][self::desc])]" is because there is a
possiblity that there is a <spec> element after <desc> in the source
XML. I do not want to match that element.

Now here is my problem. With this template, my output looks like this:

<root>

<step1>
<text>sample desc</text>

<step2>
<text>r1</text>
<spec><para>Some spec 1</para></spec>
<spec><para>Some spec 2</para></spec>
</step2>

<step2>
<text>r2</text>
<spec><para>Some spec 1</para></spec>
<spec><para>Some spec 2</para></spec>

</step2>

</step1>
</root>

So instead of <spec> repeating only under the second <item>, it
repeats under both. How do I make <spec> go under only the second
item?

I have tried <xsl:for-each-group> but with no success. So maybe there
is something that I am not understanding in this grouping technique.

Can the experts please help me on this?

Thanks in advance for all your help.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--