This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Need help with XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need help with XSLT" Watch "Need help with XSLT" New topic
Author

Need help with XSLT

Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
I have an XML which is not properly structured , i have to parse and display it with XSLT.

<information>
<records>

<personname>
<name>aaaa</name>
</personname>
<postaladdress>
<address>xhfsdfhlsd</address>
</postaladdress>

<personname>
<name>bbbb</name>
</personname>
<postaladdress>
<address>sdueiour</address>
</postaladdress>

<personname>
<name>cccc</name>
</personname>
<postaladdress>
<address>heorurot</address>
</postaladdress>

</records>
</information>

That personname and postaladdress tag will repreat different number of times in different xmls

Now I am not understanding how to pull those in that order , because i can not use a for-each there,as it will dispay one after another.

Anybody has any idea?
Thanks in advance
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Archana Annamaneni:

Now I am not understanding how to pull those in that order , because i can not use a for-each there,as it will dispay one after another.

IMHO, i dont understand why cant you use xsl:for-each, can you explain again.


Spritle Software Blogs
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
yes because if i use

<information>
<records>
<xsl:for-each select="/information/records/personname>
..........
</xsl:for-each>
<xsl:for-each select="/information/records/postaladdress>
.........
</xsl:for-each>
</information>
</records>

If i do like this it will display all the personname first and then all the postaladdress.

If i use loop inside loop postaladdress will come 4 times
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
No that will not happen.
You are mixing xml and xslt and xpath.
I would suggest you to read the beginners tutorial on xsl and xpath from w3schools site which i pointed out in your other post.
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Thanks , I been reading the w3schools.

I don't know whether you observed or not if in my xml
Personame and postaladdress had a parent tag combining those two , the problem wont be there,as you said with for-each it will disply.

But here the personname and postaladdress tags are repeating inside records , I still don't understand how it is going to work?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So you want to change your badly designed XML document into something more useful, such as this:

I don't think there is a way of using XSLT to do this - since you have no data in your personname element which links it to a specific postaladdress record. You can't rely on the positioning of one element next to another in XML, only an element's position in the docuement hierarchy and any attributes/value it might have. So basically, its not going to work. And thats a deliberate limitation of XML/XSLT.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Thanks I found the solution ,I think xpath has solution
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
I thought I found the solution , but it seems i don't.

whatever i do it is returning only the first records.

Balaji do you think there is a solution for this , if you do please let me know
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Archana Annamaneni:
Balaji do you think there is a solution for this , if you do please let me know

Can you the post the input XML and the expected output.
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Sure Here is the input xml


And here is the output code



Just added a parent tag combing two tags in the output.Let me know if you have any more questions
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
I solved the problem through JAVA using DOM
 
 
subject: Need help with XSLT
 
Similar Threads
Struts:: referring indexId of logic:iterate from javascript call
how to use variable in XSLT if condition
One-to-One mapping problem
form updation
How to parse IP address field with XSLT