473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Nested For Loops

This is a small part the xml file that I have:

<Prop Type='TEResult' Flags='0x0'>
<Prop Name='Status' Type='String' Flags='0x400000'>

<Value>Done</Value>

</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>

<Value>Scan CPU Serial Number</Value>

</Prop>
</Prop>

Now this small xml code repeats numerously throughout my xml file.I want to go through the entire xml file and extract the values (which are Done and Scan CPU Serial Number in this case) of all Prop with Name='Status' and Name='StepName'.

My output needs to look like this

<TestName>Scan CPU Serial Number</TestName>
<TestResult>Done</TestResult>

I am using nested for each statements and this is the output it is giving me:
<TestName>Scan CPU Serial Number>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult> (2)
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestResult>Done</TestResult>
<TestName><This is the next test></TestName>

What essentially should be happening is that Result( 2) should come below my next <TestName> but because of my nested for each loops, it goes through the entire xml file, prints all the results and then goes back to the next test, then again all the results instead of <Test><Its Result> <Test><Its Result>?

How can I solve this?
Jul 10 '08 #1
1 1789
jkmyoung
2,057 Expert 2GB
It'd be easier if we could see your code, but it'd probably be something like:

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="Prop">
  2. <xsl:choose>
  3.   <xsl:when test="@Name  = 'StepName'">
  4.      <TestName><xsl:value-of select="Value"/></TestName>
  5.   </xsl:when>
  6.   <xsl:when test="@Name = 'Status'">
  7.       <TestResult><xsl:value-of select="Value"/></TestResult>
  8.   </xsl:when>
  9. </xsl:choose>
  10. </xsl:template>
  11.  
Use an apply-templates, instead of a for-each loop.
You haven't nested the results inside a step node, so you can't use for-each like that either.

If you really want to, or are changing your code to become proper structured xml (eg, where your steps are grouped together under one node) you could use a key.

Expand|Select|Wrap|Line Numbers
  1. <xsl:key name="StepName" match="Prop" select="preceding-sibling::Prop[@Name='StepName'][1]"/>
  2.  
  3. <xsl:for-each select="Prop[@Name = 'StepName']>
  4. Put test name here.
  5.   <xsl:for-each select="key('StepName', .)">
  6.   Test Values here.
  7.    </xsl:for-each>
  8. </xsl:for-each>
  9.  
Jul 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.