469,277 Members | 2,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

Matching nested loop values

Hello, I am trying to match an Id number that is shared across 2 xml files.I seem to be do the right thing bu it won`t display a match.Can you please help me find my error.
I have a problem matching the values of :
<xsl:when test= '@meterNumber = $MeterReadingNUM' >
<xsl:value-of select="@meterNumber"/>
</xsl:when>
in create_bills.xslt.How do I do this against the meterNumber in both xml files.
As you can see I want the customers with the meter numbers of matching the meterReadings xml file.

Thank you

customers.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <?xml-stylesheet type="text/xsl" href="create_bills.xslt" ?>
  3. <customerRecords>
  4.   <customerRecord customerNumber="BL343456" meterNumber="134456">
  5.  
  6.     <contactInfo>
  7.  
  8.       <name title="Mr">
  9.         <first>Bart</first>
  10.         <middle></middle>
  11.         <last>Simpson</last>
  12.       </name>
  13.  
  14.     </contactInfo>
  15.  
  16.   </customerRecord>
  17.  
  18.     <customerRecord customerNumber="BL326856" meterNumber="134452">
  19.  
  20.     <contactInfo>
  21.  
  22.       <name title="Miss">
  23.         <first>Jane</first>
  24.         <middle></middle>
  25.         <last>Simpson</last>
  26.       </name>
  27.  
  28.     </contactInfo>
  29.  
  30.   </customerRecord>
  31.  
  32. </customerRecords>
  33.  
meter_readings.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <records>
  3.   <record meterNumber="234567">
  4.     <reading date="2006-11-29">1089</reading>
  5.   </record>
  6.   <record meterNumber="134456">
  7.     <reading date="2006-11-29">67990</reading>
  8.   </record>
  9.   <record meterNumber="342566">
  10.     <reading date="2006-11-29">29011</reading>
  11.   </record>
  12. </records>
  13.  
  14.  
create_bills.xslt
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <xsl:stylesheet version="1.0"
  4.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5.   <!-- Globals  -->
  6.   <xsl:variable name='docXML_meter_readings' select='document("meter_readings.xml")'/>
  7.  <xsl:output method="html" version="4.0"/>
  8.  <xsl:template match="/">
  9. <html>
  10. <head>
  11. </head>
  12. <body>
  13. <xsl:for-each select='$docXML_meter_readings/records/record'>
  14.      <xsl:sort select='@meterNumber' data-type='number' order='ascending'/>
  15.        <xsl:variable name='MeterReadingNUM' select='@meterNumber' />   
  16.  
  17.              <xsl:for-each select='customerRecords/customerRecord'>
  18.     <xsl:choose>
  19.      <xsl:when test= '@meterNumber = $MeterReadingNUM' >
  20.                       <xsl:value-of select="@meterNumber"/>
  21.     </xsl:when>                
  22.             </xsl:choose>
  23.          </xsl:for-each>   
  24.   </xsl:for-each> 
  25.  
  26.  </body>
  27.  </html>
  28.  </xsl:template>
  29. </xsl:stylesheet>
  30.  
  31.  
Apr 19 '07 #1
6 2192
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN...

Try this updated xsl code:
[html]<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Globals -->
<xsl:variable name='docXML_meter_readings' select='document("meter_readings.xml")'/>
<xsl:output method="html" version="4.0"/>
<xsl:template match="customerRecords">
<xsl:apply-templates select="customerRecord" />
</xsl:template>

<xsl:template match="customerRecord">
<html>
<head>
</head>
<body>
<xsl:variable name='MeterReadingNUM' select='@meterNumber' />
<xsl:for-each select='$docXML_meter_readings/records/record'>
<xsl:sort select='@meterNumber' data-type='number' order='ascending'/>
<xsl:choose>
<xsl:when test= '@meterNumber = $MeterReadingNUM' >
<xsl:value-of select="@meterNumber"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>[/html]
Apr 19 '07 #2
dorinbogdan
839 Expert 512MB
Corrected and changed a little bit the title.
Apr 19 '07 #3
Thank you,I see where i was going wrong.Much appreciated,helped me understand the use of templates.
Apr 20 '07 #4
Thank you,I see where i was going wrong.Much appreciated,helped me understand the use of templates.
Hello,I now wish to connect the values in either side of my related xml files where the MeterNumber is the same.I have found a way to pass the needed values of one file to display output.
How do i Find the values needed of the second xml document?
I have the key of meterNumber and trying to gain all the other required variables form the customers.xml where I know there is a exact match.I can`t seem to display them even when using a similar for each loop.Is there a format to retrivieng this infromation?

Slightly amended to gain all required values
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="MeterReadingNUM" select="@meterNumber"/>
  2.  
  3. <xsl:for-each select="$docXML_meter_readings/records/record">
  4. <xsl:sort select="@meterNumber" data-type="number" order="ascending"/>
  5.   <xsl:choose>
  6.   <xsl:when test="@meterNumber = $MeterReadingNUM">
  7.   <xsl:call-template name="ONEBILL">
  8. <!-- Only needed values -->            
  9.   <xsl:with-param name="currentMeterRead" select="reading"/>
  10.   <xsl:with-param name="currentMeterReadDate"select="reading/@date"/>
  11.   <xsl:with-param name="BillMeterNum" select="$MeterReadingNUM"/>
  12. </xsl:call-template>
  13.  
  14.  </xsl:when>
  15. </xsl:choose>
  16. </xsl:for-each>
  17.  
  18.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. <xsl:template name="ONEBILL">
  3. <xsl:param name="currentMeterRead"/>
  4. <xsl:param name="currentMeterReadDate"/>
  5. <xsl:param name="BillMeterNum"/>
  6. <xsl:value-of select="$BillMeterNum"/> **
  7. <xsl:value-of select="$currentMeterRead"/>**
  8. <xsl:value-of select="$currentMeterReadDate"/>
  9.  
  10. <xsl:for-each select="customerRecords/customerRecord">
  11. <xsl:sort select="@meterNumber" data-type="number" order="ascending"/>
  12. <xsl:choose>
  13.   <xsl:when test="@meterNumber = $BillMeterNum">
  14.    <xsl:variable name="Name_Title" select="contactInfo/name/@title"/>
  15.    <xsl:variable name="Name_First" select="contactInfo/name/first"/>
  16.    <xsl:variable name="Name_Last" select="contactInfo/name/last"/>
  17.    <xsl:value-of select="$Name_Title"/> 
  18.     <xsl:value-of select="$Name_First"/>
  19.     <xsl:value-of select="$Name_Last"/>
  20.    </xsl:when>
  21. </xsl:choose>
  22. </xsl:for-each>
  23. </xsl:template>
  24.  
  25.  
When I execute the Customer Loop Here I cannot access the required elements to set them them as variables and display them.
This setting of customer variables from the meternumber Found is a crux of my application What is your opinion on this issue?

Thank you
Apr 20 '07 #5
I would like to pass the current node through like
<xsl:with-param name="customerRec" select="current( )/customerRecord"/>
Is it possible to pass the current node of the template match through to a named template?
Thank you
Apr 20 '07 #6
yes I have fixed my problem of passing a element of tree.It works

I have another problem on an error
'http://www.w3.org/1999/XSL/Transform' does not contain any functions
when I try only this line of code
<xsl:value-of select="xsl:date('2005-02-21') - xsl:date('2005-01-01')"/>
Why??
I have a defintion of the stylesheet as
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <!-- Globals  -->
  4. <xsl:variable name="docXML_meter_readings" select='document("meter_readings.xml")'/>
  5. <xsl:variable name='RateOfCharge_kWh' select='number(0.124)' />
  6. <xsl:variable name='SystemAccess_PerDay' select='number(0.373)' />
  7. <xsl:variable name='TenPercent' select='number(0.10)' />
  8. <xsl:variable name='NintyPercent' select='number(0.90)' />
  9. <xsl:variable name="ConstantNewAcc" select="string('New Acc')" />
  10.  
  11. <xsl:output method="html" version="4.0"/>
  12. <xsl:template match="customerRecords">
  13. <xsl:apply-templates select="customerRecord"/>
  14. </xsl:template>
  15.  
  16.  
I am using the right stylesheet schema to use functions?
Thank you
Apr 21 '07 #7

Post your reply

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

Similar topics

5 posts views Thread by Blankdraw | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.