Loop to next sibling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anii
    New Member
    • Oct 2007
    • 8

    Loop to next sibling

    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="documen t('customers.xm l')/customers/..." /> method in the XSLT document.

    I have this XML document (customer.xml):
    Code:
    <customers>
    	<customer number="__">
    		<meterNumber>
    		<title>
    		<name>
    			<firstName>
    			<middleName>
    			<lastName>
    		</name>
    		<address>
    			<streetName>
    			<suburb>
    			<postcode>
    		</address>
    		<phone>
    			<homePhone>
    			<workPhone>
    			<mobile>
    		</phone>
    		<history date="29/11/2006">
    			<meterReading>
    			<amountDue>
    			<paid>
    			<datePaid>
    			<amountPaid>
    		</history>
    // MOSTLY HAS MORE THAN ONE HISTORY ELEMENT
    	</customer>
    
    	<customer>
            ...
    	</customer>
            .
            .
            .
    </customers>
    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:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
    
    		<xsl:for-each select="meterReadings/meter">
    		<xsl:apply-templates select="document('customers.xml')" />
    
    		<hr />
    
    // HTML TABLE STUFF
    
    		<xsl:for-each select="customers/customer">
    		<tbody>
    
    			<tr>
    				<td><strong>Account Number:</strong></td>
    				<td><xsl:value-of select="@number" /></td>
    				<td><strong>Address:</strong></td>
    				<td><xsl:value-of select="address/streetName" /></td>
    			</tr>
    			<tr>
    				<td><strong>Customer Name:</strong></td>
    				<td><xsl:value-of select="name/firstName" /><xsl:text> </xsl:text><xsl:value-of select="name/lastName" /></td>
    				<td></td>
    				<td><xsl:value-of select="address/suburb" /></td>
    			</tr>
    			<tr>
    				<td></td>
    				<td></td>
    				<td></td>
    				<td><xsl:value-of select="address/postcode" /></td>
    			</tr>
    		</tbody>
    		</xsl:for-each>
    
    		</table>
    
    		<xsl:apply-templates select="date" />
    
    // HTML TABLE STUFF
    
    		<tbody>
    			<tr>
    				<td><strong>Meter Number</strong></td>
    				<td><strong>This Read</strong></td>
    				<td><strong>Previous Read</strong></td>
    				<td><strong>Usage (kWh)</strong></td>
    			</tr>
    
    			<tr>
    				<td><xsl:value-of select="@number" /></td>
    				<td><xsl:value-of select="reading" /></td>
    				<td></td>
    				<td><xsl:value-of select="reading" /></td>
    			</tr>
    		</tbody>
    					
    // HTML TABLE STUFF
    
    		</xsl:for-each>
    
    </xsl:template>
    
    <xsl:template match="date">
    	 <p><strong><u>Account Summary</u></strong> - <xsl:value-of select="." /></p>
    </xsl:template>
    
    </xsl:stylesheet>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could you provide the structure of readings.xml? It would help to see where the problem is.

    Comment

    • anii
      New Member
      • Oct 2007
      • 8

      #3
      Originally posted by jkmyoung
      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:
      Code:
      <?xml version="1.0"?>
      <customers>
      	<customer number="BL343456">
      		<meterNumber>134456</meterNumber>
      		<name title="">
      			<firstName>Bart</firstName>
      			<middleName></middleName>
      			<lastName>Simpson</lastName>
      		</name>
      		<address>
      			<streetName>45 Olive Dr.</streetName>
      			<suburb>Sydney</suburb>
      			<postcode>2000</postcode>
      		</address>
      		<phone>
      			<homePhone>02 9857 6433</homePhone>
      			<workPhone>02 9678 4455</workPhone>
      			<mobile>041 333 5678</mobile>
      		</phone>
      		<history date="2006-11-29">
      			<meterReading>62990</meterReading>
      			<amountDue>0.00</amountDue>
      			<paid>Y</paid>
      			<datePaid>29/11/2005</datePaid>
      			<amountPaid>0.00</amountPaid>
      		</history>
      		<history date="2006-02-28">
      			<meterReading>64492</meterReading>
      			<amountDue>242.21</amountDue>
      			<paid>Y</paid>
      			<datePaid>24/06/2006</datePaid>
      			<amountPaid>242.21</amountPaid>
      		</history>
      	</customer>
      
      	<customer number="TR567903">
      		<meterNumber>342566</meterNumber>
      		<name title="">
      			<firstName>Donald</firstName>
      			<middleName></middleName>
      			<lastName>Duck</lastName>
      		</name>
      		<address>
      			<streetName>12 Grey Place</streetName>
      			<suburb>Sometown</suburb>
      			<postcode>9876</postcode>
      		</address>
      		<phone>
      			<homePhone></homePhone>
      			<workPhone></workPhone>
      			<mobile>043 222 6677</mobile>
      		</phone>
      		<history date="2006-08-29">
      			<meterReading>28132</meterReading>
      			<amountDue>0.00</amountDue>
      			<paid>Y</paid>
      			<datePaid>29/08/2006</datePaid>
      			<amountPaid>0.00</amountPaid>
      		</history>
      	</customer>
       // More Customer Elements
      </customers>
      readings.xml:
      Code:
      <?xml version="1.0"?>
      
      <?xml-stylesheet type="text/xsl" href="create_bills.xsl"?>
      
      <meterReadings>
      	<meter number="234567" date="2006-11-29" reading="1089" />
      	<meter number="134456" date="2006-11-29" reading="67990" />
      	<meter number="342566" date="2006-11-29" reading="29011" />
      </meterReadings>
      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. ^___^;;
      Last edited by anii; Apr 15 '08, 07:36 PM. Reason: typos

      Comment

      Working...