473,386 Members | 1,817 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,386 software developers and data experts.

Loop to next sibling

8
Hi I'm a bit new to XML, and I was wondering if anyone could help me with my problem.

I have one XML document (readings.xml) being linked to an XSLT document via JavaScript DOM, and am linking the another (customer.xml) through the select="document('customers.xml')/customers/..." /> method in the XSLT document.

I have this XML document (customer.xml):
Expand|Select|Wrap|Line Numbers
  1. <customers>
  2.     <customer number="__">
  3.         <meterNumber>
  4.         <title>
  5.         <name>
  6.             <firstName>
  7.             <middleName>
  8.             <lastName>
  9.         </name>
  10.         <address>
  11.             <streetName>
  12.             <suburb>
  13.             <postcode>
  14.         </address>
  15.         <phone>
  16.             <homePhone>
  17.             <workPhone>
  18.             <mobile>
  19.         </phone>
  20.         <history date="29/11/2006">
  21.             <meterReading>
  22.             <amountDue>
  23.             <paid>
  24.             <datePaid>
  25.             <amountPaid>
  26.         </history>
  27. // MOSTLY HAS MORE THAN ONE HISTORY ELEMENT
  28.     </customer>
  29.  
  30.     <customer>
  31.         ...
  32.     </customer>
  33.         .
  34.         .
  35.         .
  36. </customers>
  37.  
I'm currently working on the XSLT and I'm trouble looping through each customer printing their details (from customer.xml) then also looping through the readings.xml file to get their readings.

This is what I'm aiming to acheive:

Customer #1 Details
Customer #1 Readings
---------------------------------------------
Customer #2 Details
Customer #2 Readings

etc.

So far with my XSLT I'm getting:

Customer #1 Details
Customer #2 Details
.
.
Customer #n Details

Customer #1 Readings
---------------------------------------------
Customer #1 Details
Customer #2 Details
.
.
Customer #n Details

Customer #2 Readings

etc.

I've been trying to look up loops for a while now but haven't quite got it to work with my documents to what I'm aiming to achieve.

I'm also wondering as to how you find whether two elements (not in the same XML) have the same data in them when they're in different files and different types like attribute + element.

eg. meterNumber (in customers.xml) = meterNumber (in readings.xml)
or @date (in customers.xml) = date (in readings.xml)

Any suggestions to improve my coding or a heads up to where I might be going wrong would be greatly appreciated!

Thanks a bunch! =)

Here's my XSLT:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:template match="/">
  5.  
  6.         <xsl:for-each select="meterReadings/meter">
  7.         <xsl:apply-templates select="document('customers.xml')" />
  8.  
  9.         <hr />
  10.  
  11. // HTML TABLE STUFF
  12.  
  13.         <xsl:for-each select="customers/customer">
  14.         <tbody>
  15.  
  16.             <tr>
  17.                 <td><strong>Account Number:</strong></td>
  18.                 <td><xsl:value-of select="@number" /></td>
  19.                 <td><strong>Address:</strong></td>
  20.                 <td><xsl:value-of select="address/streetName" /></td>
  21.             </tr>
  22.             <tr>
  23.                 <td><strong>Customer Name:</strong></td>
  24.                 <td><xsl:value-of select="name/firstName" /><xsl:text> </xsl:text><xsl:value-of select="name/lastName" /></td>
  25.                 <td></td>
  26.                 <td><xsl:value-of select="address/suburb" /></td>
  27.             </tr>
  28.             <tr>
  29.                 <td></td>
  30.                 <td></td>
  31.                 <td></td>
  32.                 <td><xsl:value-of select="address/postcode" /></td>
  33.             </tr>
  34.         </tbody>
  35.         </xsl:for-each>
  36.  
  37.         </table>
  38.  
  39.         <xsl:apply-templates select="date" />
  40.  
  41. // HTML TABLE STUFF
  42.  
  43.         <tbody>
  44.             <tr>
  45.                 <td><strong>Meter Number</strong></td>
  46.                 <td><strong>This Read</strong></td>
  47.                 <td><strong>Previous Read</strong></td>
  48.                 <td><strong>Usage (kWh)</strong></td>
  49.             </tr>
  50.  
  51.             <tr>
  52.                 <td><xsl:value-of select="@number" /></td>
  53.                 <td><xsl:value-of select="reading" /></td>
  54.                 <td></td>
  55.                 <td><xsl:value-of select="reading" /></td>
  56.             </tr>
  57.         </tbody>
  58.  
  59. // HTML TABLE STUFF
  60.  
  61.         </xsl:for-each>
  62.  
  63. </xsl:template>
  64.  
  65. <xsl:template match="date">
  66.      <p><strong><u>Account Summary</u></strong> - <xsl:value-of select="." /></p>
  67. </xsl:template>
  68.  
  69. </xsl:stylesheet>
  70.  
Apr 14 '08 #1
2 3606
jkmyoung
2,057 Expert 2GB
Could you provide the structure of readings.xml? It would help to see where the problem is.
Apr 15 '08 #2
anii
8
Could you provide the structure of readings.xml? It would help to see where the problem is.
Hi, sure! I've modified them all the documents slightly since my last post but here they are:

customer.xml:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <customers>
  3.     <customer number="BL343456">
  4.         <meterNumber>134456</meterNumber>
  5.         <name title="">
  6.             <firstName>Bart</firstName>
  7.             <middleName></middleName>
  8.             <lastName>Simpson</lastName>
  9.         </name>
  10.         <address>
  11.             <streetName>45 Olive Dr.</streetName>
  12.             <suburb>Sydney</suburb>
  13.             <postcode>2000</postcode>
  14.         </address>
  15.         <phone>
  16.             <homePhone>02 9857 6433</homePhone>
  17.             <workPhone>02 9678 4455</workPhone>
  18.             <mobile>041 333 5678</mobile>
  19.         </phone>
  20.         <history date="2006-11-29">
  21.             <meterReading>62990</meterReading>
  22.             <amountDue>0.00</amountDue>
  23.             <paid>Y</paid>
  24.             <datePaid>29/11/2005</datePaid>
  25.             <amountPaid>0.00</amountPaid>
  26.         </history>
  27.         <history date="2006-02-28">
  28.             <meterReading>64492</meterReading>
  29.             <amountDue>242.21</amountDue>
  30.             <paid>Y</paid>
  31.             <datePaid>24/06/2006</datePaid>
  32.             <amountPaid>242.21</amountPaid>
  33.         </history>
  34.     </customer>
  35.  
  36.     <customer number="TR567903">
  37.         <meterNumber>342566</meterNumber>
  38.         <name title="">
  39.             <firstName>Donald</firstName>
  40.             <middleName></middleName>
  41.             <lastName>Duck</lastName>
  42.         </name>
  43.         <address>
  44.             <streetName>12 Grey Place</streetName>
  45.             <suburb>Sometown</suburb>
  46.             <postcode>9876</postcode>
  47.         </address>
  48.         <phone>
  49.             <homePhone></homePhone>
  50.             <workPhone></workPhone>
  51.             <mobile>043 222 6677</mobile>
  52.         </phone>
  53.         <history date="2006-08-29">
  54.             <meterReading>28132</meterReading>
  55.             <amountDue>0.00</amountDue>
  56.             <paid>Y</paid>
  57.             <datePaid>29/08/2006</datePaid>
  58.             <amountPaid>0.00</amountPaid>
  59.         </history>
  60.     </customer>
  61.  // More Customer Elements
  62. </customers>
  63.  
readings.xml:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <?xml-stylesheet type="text/xsl" href="create_bills.xsl"?>
  4.  
  5. <meterReadings>
  6.     <meter number="234567" date="2006-11-29" reading="1089" />
  7.     <meter number="134456" date="2006-11-29" reading="67990" />
  8.     <meter number="342566" date="2006-11-29" reading="29011" />
  9. </meterReadings>
  10.  
And my updated XSLT -- you can find it here (its a bit long -- and very messy)

Please let me know if that wasn't what you were asking for. ^___^;;
Apr 15 '08 #3

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

Similar topics

3
by: Peter Rohleder | last post by:
Hi, I'm using a style-sheet where I make use of the XPATH-"following-sibling"-expression. The part which makes problems looks similar to the following code: --------------------------- ...
1
by: Philipp Lenssen | last post by:
I have some chapters a document goes through, so at the end of each document I want to output "Continue with XYZ", where XYZ is the next-sibling of the current one I select. However I don't know...
2
by: Michael K?nig | last post by:
Hello, I've an XML-file structured like this <table> <tr> <td>Nombre:</td> <td>Joseph</td> <td>Apellido:</td> <td>Ratzinger</td>
0
by: Elaine | last post by:
I have a truly curious problem with HtmlHelp and Sibling Mode in Visual C++ ..Net 2003 in an MFC app. Sibling mode allows the help viewer to display on top of the app, but if the app is clicked,...
1
by: paul.rusu | last post by:
XSLT: how to find 2nd next sibling?
11
by: localpricemaps | last post by:
i have some html which looks like this where i want to scrape out the href stuff (the www.cnn.com part) <div class="noFood">Cheese</div> <div class="food">Blue</div> <a class="btn" href =...
1
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL My XSL file and XML below I needed to do a distinct which is ok on the first node "Code" For the "programDescription" i did below which gets the...
2
by: davidw | last post by:
I have flat xml like this <field name="a1"/> <field name="a2" merge="true"/> <field name="a3"/> <field name="a4"/> <field name="a5" merge="true"/> <field name="a6" merge="true"/> <field...
3
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
12
by: apicard | last post by:
I have a simple document like this: <Accept> <XXXX/> <Token image="From"/> <Date value="2007-01-01"/> <Token image="To"/> <Date value="2007-01-01"/> </Accept>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.