Hi,
Please use this stylesheet and see if it fits your needs.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"></xsl:output>
<xsl:template match="/root">
<xsl:apply-templates select="name[(position() mod 3 = 1) or . =
'1']">
<xsl:with-param name="ctr" select="0"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="name">
<xsl:param name="ctr"/>
<xsl:variable name="sum" select="count(following-sibling::name[. =
preceding-sibling::name/text()])"/>
<xsl:if test="$ctr <= $sum">
<Loop>
<xsl:copy-of select="."/>
<xsl:apply-templates select="following-sibling::name[1]">
<xsl:with-param name="ctr" select="$ctr+1"/>
</xsl:apply-templates>
</Loop>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
cheers,
prakash
ma he
<mahendranvm(_at_)yah To:
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
oo.co.in> cc: (bcc:
omprakash.v/Polaris)
Subject: [xsl] urgent..creating
loops in output xml based on input xml
09/12/2005 02:44 values..
PM
Please respond
to xsl-list
Hi all,
I am new to the whole xsl world and I have trouble
creating an xsl. This is urgent and I have less time
to do research. so plz help me with this asap.
This is my requirement,
the input xml will be in this format,
<root>
<name>1</name>
<name>2</name>
<name>3</name>
<name>1</name>
<name>2</name>
<name>1</name>
</root>
The possible values for name are only 1,2 and 3. I
should supply an xsl which will be used to transform
this xml into another xml in the following format,
<root>
<Loop>
<name>1</name>
<Loop>
<name>2</name>
<Loop>
<name>3</name>
</Loop>
</Loop>
</Loop>
<Loop>
<name>1</name>
<Loop>
<name>2<name>
</Loop>
</Loop>
<Loop>
<name>1</name>
</Loop>
</root>
In the input xml, there will not be a 2 without 1
preceding it. similarly there wont be a 3 without a 2
preceding it. But there could be more than one 1 or 2
or 3, one after the other. Here's another example,
<root>
<name>1</name>
<name>2</name>
<name>2</name>
</root>
Here the output will be
<root>
<Loop>
<name>1</name>
<Loop>
<name>2</name>
</Loop>
<Loop>
<name>2</name>
</Loop>
</Loop>
</root>
I thought of using the following algorithm to
determine when to open a loop element and when to
close a loop,
declare variables L1 = no, L2 = no and L3 = no
For each <root> in output xml
{
If (name = 1)
{
If (L3 = yes) then in output xml, close loop
"</Loop>
If (L2 = yes) then in output xml, close loop
"</Loop>
If (L1 = yes) then in output xml, close loop
"</Loop>
Open Loop in output xml "<Loop>"
}
If (name = 2)
{
If (L3 = yes) then in output xml, close loop
"</Loop>
If (L2 = yes) then in output xml, close loop
"</Loop>
Open Loop in output xml "<Loop>"
}
If (name = 3)
{
If (L3 = yes) then in output xml, close loop
"</Loop>
Open Loop in output xml "<Loop>"
}
}
But here, since variable values once assigned cannot
be reassigned, I am not able to implement this algo.
in xsl. Help me with some other logic that will be
able to achieve the same. (all I want here is to do
the following, there will be an input xml file where
all elements are in the same level. In the output
file, I want these elements nested one inside another,
based on their values.). Thanks in advance for ur
help.
-mahe
__________________________________________________________
Yahoo! India Matrimony: Find your partner now. Go to
http://yahoo.shaadi.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>
--~--
This e-Mail may contain proprietary and confidential information and is sent
for the intended recipient(s) only.
If by an addressing or transmission error this mail has been misdirected to
you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction,
dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its
attachment other than by its intended recipient/s is strictly prohibited.
Visit Us at http://www.polaris.co.in
--~------------------------------------------------------------------
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>
--~--