• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSLT Loops - Temp Variables?

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Experts,

I have a XML snippet

<?xml version="1.0" encoding="UTF-8"?>
<XML1>
  <CMD>
     <ID>1</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
  </CMD>
  <CMD>
     <ID>2</ID>
     <LOCATION>Bangalore</LOCATION>
     <PIN>xxxxx</PIN>
  </CMD>
  <CMD>
     <ID>3</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
  </CMD>
  <CMD>
     <ID>4</ID>
     <LOCATION>Mumbai</LOCATION>
     <PIN>112200</PIN>
  </CMD>
</XML1>

I want to add a link node to above CMD tags such that when 2 or more node have location match, I create a link node that has the ID of the matching node. So I want my Target XML to be as below

<?xml version="1.0" encoding="UTF-8"?>
<XML1>
  <CMD>
     <ID>1</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
  </CMD>
  <CMD>
     <ID>2</ID>
     <LOCATION>Bangalore</LOCATION>
     <PIN>xxxxx</PIN>
  </CMD>
  <CMD>
     <ID>3</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
 <LINK>
<ID>1</ID>
 </LINK>
  </CMD>
  <CMD>
     <ID>4</ID>
     <LOCATION>Mumbai</LOCATION>
     <PIN>112200</PIN>
  </CMD>
</XML1>

In my XSLT i have created 2 for-each loops against the same XML.
After first loop, I create first CMD Tag and then in the second loop, if I find a match I add link node to subsequent nodes.

I need to make sure in the first loop that if any nodes has already been processed with link node I need to ignore them. My question is do we have some mechanism in XSLT where my I can check for a variable for ID's that has already been processed, so that I can ignore them in my first loop?

Not sure if my question makes sense.
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<?xml version="1.0" encoding="UTF-8"?>
<XML1>
  <CMD>
     <ID>1</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
  </CMD>
  <CMD>
     <ID>2</ID>
     <LOCATION>Bangalore</LOCATION>
     <PIN>xxxxx</PIN>
  </CMD>
  <CMD>
     <ID>3</ID>
     <LOCATION>Delhi</LOCATION>
     <PIN>112200</PIN>
<LINK>
<ID>1</ID>
 </LINK>

  </CMD>
  <CMD>
     <ID>4</ID>
     <LOCATION>Mumbai</LOCATION>
     <PIN>112200</PIN>
  </CMD>
</XML1>

Re-Pasting my target XML snippet to be more clear.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can produce what you've conceived and then sketched the two loops in xslt, rightly or wrongly, may be I would understand what you really have in mind.
 
Marshal
Posts: 28264
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
g tsuji is right, your idea that you have to write a procedural loop with mutable variables is just going down the wrong road. XSLT doesn't have mutable variables and it isn't even a procedural language. But there's likely an XPath expression which will do the job for you, if we knew when these ID elements were to be inserted.
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help?

 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I don't think you actually want to test ID and LOCATION (not location, name is case-sensitive). But instead you want to check LOCATION and PIN. And then secondly, xsl:choose cannot be a child of xsl:if, it can only be child of xsl:when.

In any case, you can do it like this. (The code explicit is for your study purpose, that's how you'd get the most out of it.)

(I suppose CMD in the original xml be command which appears in the xsl:for-each.)
 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line of mine should be taken out, made too carelessly.

g tsuji wrote:And then secondly, xsl:choose cannot be a child of xsl:if, it can only be child of xsl:when.

 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I have to change and had forgotten to change the CMD to command at another place:


And then another reading of having ID (not repeated) as another condition too, you can do like this.
 
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic