XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Surekha PalaparthySubject: Looping in XSLT
Author: Surekha Palaparthy
Date: 16 Aug 2005 04:59 AM
Hi
I need to transform an XML file into another XML .I have two questions regarding making XSLT file for this.

Q1)There are elements in the XML file which specify the count through which the loop should be processed.I dont know as to how to go ahead with the looping inside an XSLT.Can anyone please help me out in this.The count is equal to the value of "CountVal" element in the sample XML file given.
The loop should be in such a way that i can get the respective XSL(CountVal)Val element's value .Similar is the case with CountRet.Also notice that since i have the count of CountRet as 3,the output XML file has XSLRet val as "-",though there is a XSLT4Val in the original XMl file .


Q2)As you motice ,there are two other elements VALIndex and RETIndex in the original XMl file which hold an array of values.

I find this to be toooo tricky..Not up with a solution....Please help me as early as possible..Waiting for the input from your side.


This is the original XML file.

<?xml version="1.0"?>
<Main >
<Step1>
<Step4>
<Apply>true</Apply>
<Name>CountVal</Name>
<Value>5</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>CountRet</Name>
<Value>3</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL1Val</Name>
<Value>124</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL2Val</Name>
<Value>13</Value>
<Step4>
<Apply>true</Apply>
<Name>XSL3Val</Name>
<Value>12</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL4Val</Name>
<Value>14</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL5Val</Name>
<Value>124</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL6Val</Name>
<Value>12</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL7Val</Name>
<Value>12</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>Xsl1Ret</Name>
<Value>10</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL2Ret</Name>
<Value>124</Value>
<Step4>
<Apply>true</Apply>
<Name>XSL3Ret</Name>
<Value>12</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>XSL4Ret</Name>
<Value>14</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>VALIndex</Name>
<Value>3;1;2;4;0;0;0;0;0;0;</Value>
</Step4>
<Step4>
<Apply>true</Apply>
<Name>RETIndex</Name>
<Value>2;4;3;1;0;0;0;0;0;0;</Value>
</Step4>
</Step1>
</Main>

The output xml file that I want to retrieve is also given below.

Output XML:
-------------------------------------------------------
<?xml version="1.0"?>
<Main >
<Table1>
<XSLVal>124</XSLVal>
<XSLRet>10</XSLVal>
<VALIndex>3</VALIndex>
<RETIndex>2</RETIndex>
</Table1>
<Table2>
<XSLVal>13</XSLVal>
<XSLRet>124</XSLVal>
<VALIndex>1</VALIndex>
<RETIndex>4</RETIndex>
</Table2>
<Table3>
<XSLVal>12</XSLVal>
<XSLRet>12</XSLVal>
<VALIndex>2</VALIndex>
<RETIndex>3</RETIndex>
</Table3>
<Table3>
<XSLVal>14</XSLVal>
<XSLRet>-</XSLVal>
<VALIndex>4</VALIndex>
<RETIndex>1</RETIndex>
</Table3>
</Main>


Thanks in Advance

Postnext
Ivan PedruzziSubject: Looping in XSLT
Author: Ivan Pedruzzi
Date: 16 Aug 2005 09:53 AM

The attached XSLT should give you a start.

Hope this helps
Ivan Pedruzzi
Stylus Studio team


Documentxsltval.xsl

Posttop
Surekha PalaparthySubject: Looping in XSLT
Author: Surekha Palaparthy
Date: 17 Aug 2005 09:40 AM
Thanks a lot for your help!!
This is indeed a great help for a beginner .
I started implementing the way as suggested by you.

I am attaching the OriginalXml and xsl files.
As you notice ,the xml output file generated is a bit different from what i expect.I want only the first four tables to be pritned,instead it repeats itself.Can you suggest as to why its happening so

Please Copy paste into a notepad .These are working fine at my side
/************************************
Original xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="EXTPPOST2.xsl"?>
<Main >
<Main1>
<Main2>
<Main3>
<Content>
<Name>Count</Name>
<Value>4</Value>
</Content>
<Content>
<Name>Array</Name> <Value>1;2;3;4;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;</Value>
</Content>
<Content>
<Name>Item1Val</Name>
<Value>10</Value>
</Content>
<Content>
<Name>Item2Val</Name>
<Value>20</Value>
</Content>
<Content>
<Name>Item3Val</Name>
<Value>30</Value>
</Content>
<Content>
<Name>Item4Val</Name>
<Value>40</Value>
</Content>
<Content>
<Name>Item5Val</Name>
<Value>50</Value>
</Content>
<Content>
<Name>Item6Val</Name>
<Value>60</Value>
</Content>
<Content>
<Name>Item7Val</Name>
<Value>70</Value>
</Content>
<Content>
<Name>Item8Val</Name>
<Value>80</Value>
</Content>
<Content>
<Name>Item9Val</Name>
<Value>90</Value>
</Content>
</Main3>
</Main2>
</Main1>
</Main>
/****************************************************

Xsl file

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

<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

<xsl:variable name= "MainCount" select="number(Main/Main1/Main2/Main3/Content[Name='Count']/Value/text())"/>

<xsl:template match="/">
<NewDataSet>
<xsl:apply-templates select="Main/Main1/Main2/Main3/Content">
<xsl:with-param name="NoCount" select="$MainCount"/>
<xsl:with-param name="Array" select="Main/Main1/Main2/Main3/Content[Name='Array']/Value/text()"/>
</xsl:apply-templates>
</NewDataSet>
</xsl:template>

<xsl:template match="Content">
<xsl:param name="NoCount"/>
<xsl:param name="Array"/>

<xsl:if test="$NoCount > 0" >
<Table1>
<Count>
<xsl:value-of select="$MainCount - $NoCount + 1"/>
</Count>
<Array>
<xsl:value-of select="substring-before($Array,';')"/>
</Array>
<Item>
<xsl:value-of select="following-sibling::Content[Name=concat('Item',$MainCount - $NoCount + 1,'Val')]/Value"/>
</Item>
</Table1>

<xsl:apply-templates select="following-sibling::Content[Name=concat('Item',$MainCount - $NoCount + 1,'Val')]">
<xsl:with-param name="NoCount" select="$NoCount - 1"/>
<xsl:with-param name="Array" select="substring-after($Array,';')"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
/*****************************************************************
Output xml that I view
<NewDataSet>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item>10</Item>
</Table1>
- <Table1>
<Count>2</Count>
<Array>2</Array>
<Item>20</Item>
</Table1>
- <Table1>
<Count>3</Count>
<Array>3</Array>
<Item>30</Item>
</Table1>
- <Table1>
<Count>4</Count>
<Array>4</Array>
<Item>40</Item>
</Table1> <!--I want the data to be pritned only till here
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item>10</Item>
</Table1>
- <Table1>
<Count>2</Count>
<Array>2</Array>
<Item>20</Item>
</Table1>
- <Table1>
<Count>3</Count>
<Array>3</Array>
<Item>30</Item>
</Table1>
- <Table1>
<Count>4</Count>
<Array>4</Array>
<Item>40</Item>
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
- <Table1>
<Count>1</Count>
<Array>1</Array>
<Item />
</Table1>
</NewDataSet>

Thanks once again

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.