Two Laptop Bag*
The moose likes XML and Related Technologies and the fly likes need to generate some numbers using loop, the number of times condition met Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "need to generate some numbers using loop, the number of times condition met " Watch "need to generate some numbers using loop, the number of times condition met " New topic
Author

need to generate some numbers using loop, the number of times condition met

vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
In my xslt there is one condition with TAG called NO (generates some number everytime)
<xsl:element name="SCHEDULE_ELEMENT">
<xsl:variable name="preceding_element" select="preceding-sibling::SCHEDULE_ELEMENT[1]"/>
<xsl:element name="NO">
<xsl:value-of select="NO"/> </xsl:element>

But the number in xml comes very random like
1
1
3
1
5 or anything
now i need to generate a list of numbers,in creasing order from 1 to any (may be using for loop) the number of times this condition met. It can be any number of times.

and if it gets stored anywhere (in kind of buffer or so) the time NO tag ends it should go back to 0 or 1, so that for next xml, it won't create anything starting from, except 0 or 1.

I tried something like this:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xslutput method="xml"/>
<xsl:variable name="NO"></xsl:variable>

<xsl:template match="NO">
<xsl:param name="no"/>
<xsl:if test="$no = 1">
<!--to generate the next value-->
<xsl:for-each select="NO" >
<xsl:value-of select="NO + 1"></xsl:value-of>
</xsl:for-each>
<xsl:call-template name="for-loop">
<xsl:with-param name="no" select="1"/>
<xsl:with-param name="increment" select="1"/>
<xsl:with-param name="operator" select= "'='" />
<xsl:with-param name="testValue" select="1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="for-loop">
<xsl:param name="no" select="1"/>
<xsl:param name="increment" select="1"/>
<xsl:param name="operator" select="'='"/>
<xsl:param name="testValue" select="1"/>
<xsl:param name="iteration" select="1"/>

<xsl:variable name="testPassed">
<xsl:choose>
<xsl:when test="starts-with($operator,'=')">
<xsl:if test="$no = $testValue">
<xsl:text>true</xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="starts-with($operator,'>=')">
<xsl:if test="$no >= $testValue">
<xsl:text>true</xsl:text>
</xsl:if>
</xsl:when>
<xsltherwise>
<xsl:message terminate="yes">
</xsl:message>
</xsltherwise>
</xsl:choose>
</xsl:variable>
</xsl:template>


</xsl:stylesheet>

Please guide me to proceed further.

Vijendra


Vijendra
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, I read your explanation four times and I still couldn't quite understand it. And even XSLT code is easier to read if it's indented, so your code wasn't all that helpful. (You might want to edit your post and put the code into the CODE tags, then indent it, for other readers.)

But what I think I got was this: you want to execute a template with values of I running from zero to N. Or 1 to N, doesn't really matter. You had some stuff about NO which I didn't quite understand because it didn't refer to anything in your example, but it looked like you were trying to change the value of a variable. You can't do that in XSLT. And you can't change anything in the input document either, if that was what you meant.

You need a recursive template, I think. This technique is well covered in books about XSLT, but it isn't covered by so-called tutorial sites on the web that just rehash what the XSLT elements do. Like this (I didn't do the XSLT verboseness though):



Then to get it started you just do template(1, MAX) and the recursive processing does the loop.
vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
Well you understood true.
I have NO tag with some different values (its a unique number always coming from some input xml file) but the xml file was having recursive structure, so after making it flat, the values of No tag are not quite in sequence. The values are may be like 1,2, 1,2,3, 1,5,6,7, 8,9,1,2,3 or so on. I just want a sequence to be followed, for that purpose i was trying to generate some loop with standard numbers. like 1,2,3,4,5,6,7,8,9
& so on. I have one more option, from input file, i can ignore getting the NO(Number) tag values and put my own.If it is possible.
Please suggest me.
vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
trying to generate values for NO tag like this:
<xsl:template name="SCHEDULE_ELEMENT">
<xsl:element name="SCHEDULE_ELEMENT">
<xsl:variable name="preceding_element" select="preceding-sibling::SCHEDULE_ELEMENT[1]"/>
<!--- something inside -->
<xsl:element name="NO">
<xsl:for-each select="SCHEDULE_ELEMENT_INFO">
<xsl:variable name="no" select="."/>
<xsl:apply-templates select="forloop"/>
</xsl:for-each>
</xsl:element>

Now for loop part:--

<xsl:template name="forloop">
<xsl:param name="no" select="1"/>
<xsl:param name="increment" select="1"/>
<xsl:param name="operator" select="'='"/>
<xsl:param name="testValue" select="1"/>
<xsl:param name="iteration" select="1"/>

<xsl:variable name="testPassed">
<xsl:choose>
<xsl:when test="starts-with($operator,'=')">
<xsl:if test="$no = $testValue">
<xsl:text>true</xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="starts-with($operator,'>=')">
<xsl:if test="$no >= $testValue">
<xsl:text>true</xsl:text>
</xsl:if>
</xsl:when>
<xsltherwise>
<xsl:message terminate="yes">
</xsl:message>
</xsltherwise>
</xsl:choose>
</xsl:variable>

I know this is not the best way to get my answer.

Please suggest me something more proper.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, now I'm confused. You say "I have NO tag with some different values" but then you posted some code which is trying to generate NO elements. So which is it? The NO elements are in the input file or you want to generate them into the output file?

And I'm also confused about this NO element. Or these NO elements. Is there only one, and it contains a text element that looks like "1,2,3"? Or are there many, and each contains only a single number?

And please indent your code. Use the CODE tags to do that. There's a convenient button you can use to do that.
vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
Hi starting form output values for NO tag what i normally get is :

and it goes on ...
this is coming directly from xml (input file ) to output after transformation.

Now what i need is to generate new numbers from 1 to n, so that i can put those numbers instead of this ugly looking structure which makes technically no sense.

Possibilities are replace orginal values (don't read from xml)
and put new values by yourself.(generating some values using loop or increment order or whatever feasible in xslt.)

Can you please provide me some help on this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Not really. I still can't figure out whether you "get" that as part of your input document or whether you "get" it as a result of the transformation. I asked that already but your answer didn't answer the question.
vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
In normal words the input xml does have NO tag with values in recursive order. So the structure, which shows the contents will be something like


Here i have shown only 1 level of recursion.
now after making this structure flat
we can expect the output to be shown in output xml as


But what i really need to modify is



and so on.

DO you think it is really possible.
Or by using any loop which can ignore real value of NO tag initially
and then we generate our own values and insert them in output xml.
Am i clear now.
YOu can ask me if any clarification required.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need to generate some numbers using loop, the number of times condition met