Matching nested loop values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andyalean1
    New Member
    • Apr 2007
    • 6

    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 = $MeterReadingNU M' >
    <xsl:value-of select="@meterN umber"/>
    </xsl:when>
    in create_bills.xs lt.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
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="create_bills.xslt" ?>
    <customerRecords>
      <customerRecord customerNumber="BL343456" meterNumber="134456">
       
        <contactInfo>
    	
          <name title="Mr">
            <first>Bart</first>
            <middle></middle>
            <last>Simpson</last>
          </name>
        
        </contactInfo>
    
      </customerRecord>
      
        <customerRecord customerNumber="BL326856" meterNumber="134452">
       
        <contactInfo>
    	
          <name title="Miss">
            <first>Jane</first>
            <middle></middle>
            <last>Simpson</last>
          </name>
        
        </contactInfo>
    
      </customerRecord>
      
    </customerRecords>
    meter_readings. xml

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <records>
      <record meterNumber="234567">
        <reading date="2006-11-29">1089</reading>
      </record>
      <record meterNumber="134456">
        <reading date="2006-11-29">67990</reading>
      </record>
      <record meterNumber="342566">
        <reading date="2006-11-29">29011</reading>
      </record>
    </records>
    create_bills.xs lt
    Code:
     
    <?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="/">
    <html>
    <head>
    </head>
    <body>
    <xsl:for-each select='$docXML_meter_readings/records/record'>
         <xsl:sort select='@meterNumber' data-type='number' order='ascending'/>
           <xsl:variable name='MeterReadingNUM' select='@meterNumber' />   
    
                 <xsl:for-each select='customerRecords/customerRecord'>
    	<xsl:choose>
    	 <xsl:when test= '@meterNumber = $MeterReadingNUM' >
                          <xsl:value-of select="@meterNumber"/>
    	</xsl:when>				
                </xsl:choose>
             </xsl:for-each>   
      </xsl:for-each> 
    
     </body>
     </html>
     </xsl:template>
    </xsl:stylesheet>
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Welcome to TheScripts TSDN...

    Try this updated xsl code:
    [html]<?xml version="1.0" encoding="utf-8"?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <!-- Globals -->
    <xsl:variable name='docXML_me ter_readings' select='documen t("meter_readin gs.xml")'/>
    <xsl:output method="html" version="4.0"/>
    <xsl:template match="customer Records">
    <xsl:apply-templates select="custome rRecord" />
    </xsl:template>

    <xsl:template match="customer Record">
    <html>
    <head>
    </head>
    <body>
    <xsl:variable name='MeterRead ingNUM' select='@meterN umber' />
    <xsl:for-each select='$docXML _meter_readings/records/record'>
    <xsl:sort select='@meterN umber' data-type='number' order='ascendin g'/>
    <xsl:choose>
    <xsl:when test= '@meterNumber = $MeterReadingNU M' >
    <xsl:value-of select="@meterN umber"/>
    </xsl:when>
    </xsl:choose>
    </xsl:for-each>

    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>[/html]

    Comment

    • dorinbogdan
      Recognized Expert Contributor
      • Feb 2007
      • 839

      #3
      Corrected and changed a little bit the title.

      Comment

      • andyalean1
        New Member
        • Apr 2007
        • 6

        #4
        Thank you,I see where i was going wrong.Much appreciated,hel ped me understand the use of templates.

        Comment

        • andyalean1
          New Member
          • Apr 2007
          • 6

          #5
          Originally posted by andyalean1
          Thank you,I see where i was going wrong.Much appreciated,hel ped 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
          Code:
          <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:call-template name="ONEBILL">
          <!-- Only needed values -->			
            <xsl:with-param name="currentMeterRead" select="reading"/>
            <xsl:with-param name="currentMeterReadDate"select="reading/@date"/>
            <xsl:with-param name="BillMeterNum" select="$MeterReadingNUM"/>
          </xsl:call-template>
          
           </xsl:when>
          </xsl:choose>
          </xsl:for-each>
          Code:
          <xsl:template name="ONEBILL">
          <xsl:param name="currentMeterRead"/>
          <xsl:param name="currentMeterReadDate"/>
          <xsl:param name="BillMeterNum"/>
          <xsl:value-of select="$BillMeterNum"/> **
          <xsl:value-of select="$currentMeterRead"/>**
          <xsl:value-of select="$currentMeterReadDate"/>
          					
          <xsl:for-each select="customerRecords/customerRecord">
          <xsl:sort select="@meterNumber" data-type="number" order="ascending"/>
          <xsl:choose>
            <xsl:when test="@meterNumber = $BillMeterNum">
             <xsl:variable name="Name_Title" select="contactInfo/name/@title"/>
             <xsl:variable name="Name_First" select="contactInfo/name/first"/>
             <xsl:variable name="Name_Last" select="contactInfo/name/last"/>
             <xsl:value-of select="$Name_Title"/> 
              <xsl:value-of select="$Name_First"/>
              <xsl:value-of select="$Name_Last"/>
             </xsl:when>
          </xsl:choose>
          </xsl:for-each>
          </xsl:template>
          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

          Comment

          • andyalean1
            New Member
            • Apr 2007
            • 6

            #6
            I would like to pass the current node through like
            <xsl:with-param name="customerR ec" select="current ( )/customerRecord"/>
            Is it possible to pass the current node of the template match through to a named template?
            Thank you

            Comment

            • andyalean1
              New Member
              • Apr 2007
              • 6

              #7
              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:dat e('2005-02-21') - xsl:date('2005-01-01')"/>
              Why??
              I have a defintion of the stylesheet as
              Code:
              <?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:variable name='RateOfCharge_kWh' select='number(0.124)' />
              <xsl:variable name='SystemAccess_PerDay' select='number(0.373)' />
              <xsl:variable name='TenPercent' select='number(0.10)' />
              <xsl:variable name='NintyPercent' select='number(0.90)' />
              <xsl:variable name="ConstantNewAcc" select="string('New Acc')" />
                
              <xsl:output method="html" version="4.0"/>
              <xsl:template match="customerRecords">
              <xsl:apply-templates select="customerRecord"/>
              </xsl:template>
              I am using the right stylesheet schema to use functions?
              Thank you

              Comment

              Working...