Link to home
Start Free TrialLog in
Avatar of Neil Thompson
Neil ThompsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

get node values of xml file via xslt

Hi

I have the following XSLT that transforms the XML below to give me a text file, however I need to grab the values of numberOf and valueOf in the node Totals to use in the footer template but cant seem to get it to work.

Any ideas please? preferably altering my XSLT as I'm new to this and winging it a bit!

Thanks
Neil

<?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
      <xsl:output method="text" indent="no" />      
     
<xsl:template match="/">
    <xsl:call-template name="header"/>
    <xsl:apply-templates select="//ReturnedDebitItem"/>
    <xsl:call-template name="footer"/>
</xsl:template>
     
<xsl:template name="header">    
    <xsl:text>header
</xsl:template>

<xsl:template match="ReturnedDebitItem">
    <xsl:text>aaaaaaaaaaaaaa</xsl:text>
    <xsl:value-of select="translate(PayerAccount/@sortCode,'-','')" />        
    <xsl:value-of select="PayerAccount/@number" />
    <xsl:value-of select="@returnCode" />        
    <xsl:variable name="zero" select="'00000000000'"/> <!-- 11 -->
    <xsl:variable name="vo" select="translate(@valueOf,'.','')" />
    <xsl:variable name="res">
        <xsl:value-of select="substring($zero,1,(string-length($zero) - string-length($vo)))"/>
        <xsl:value-of select="$vo"/>
    </xsl:variable>
    <xsl:value-of select="$res"/>        
    <xsl:text>ABC DIRECT </xsl:text>
    <xsl:text>bbbbbbbbb</xsl:text>    
    <xsl:value-of select="PayerAccount/@ref" />
    <xsl:value-of select="PayerAccount/@name" />    
    <xsl:text>
    </xsl:text>
</xsl:template>  
 
<xsl:template name="footer">    
    <xsl:text>footer1 </xsl:text>  
    ######## need valueOf from Totals node here #########
    <xsl:text> footer2</xsl:text>        
</xsl:template>
   
</xsl:stylesheet>





<?xml version="1.0" encoding="UTF-8"?>
<BACSDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="newbacs-advices.xsd">
  <Data>
    <ARUDD>
      <Header reportType="abc123" adviceNumber="123" currentProcessingDate="2007-07-27"></Header>
      <AddresseeInformation name="test"></AddresseeInformation>
      <ServiceLicenseInformation userName="test" userNumber="123"></ServiceLicenseInformation>
      <Advice>
        <OriginatingAccountRecords>
          <OriginatingAccountRecord>
            <OriginatingAccount name="ABC HHGGDD P" number="22334455" sortCode="00-00-00" type="0" bankName="My Bank" branchName="High ROAD"></OriginatingAccount>
            <ReturnedDebitItem ref="PAR123            " transCode="66" returnCode="333" returnDescription="asdsad" originalProcessingDate="2007-07-25" valueOf="25.00" currency="GBP"><PayerAccount number="12345678" ref="P00531            " name="asdfg &amp; dsfsdf" sortCode="01-01-01" bankName="My Bank 1" branchName="HIGH STREET"></PayerAccount></ReturnedDebitItem>
            <ReturnedDebitItem ref="PAR123            " transCode="77" returnCode="444" returnDescription="asdasd" originalProcessingDate="2007-07-25" valueOf="75.00" currency="GBP"><PayerAccount number="87654321" ref="P00674            " name="zxccxz &amp; dsfsdf" sortCode="02-02-02" bankName="My Bank 2" branchName="LOW STREET"></PayerAccount></ReturnedDebitItem>
            <Totals numberOf="2" valueOf="100..00" currency="GBP"></Totals>
          </OriginatingAccountRecord>
        </OriginatingAccountRecords>
      </Advice>
    </ARUDD>
  </Data>
  <SignatureMethod></SignatureMethod>
  <Signature></Signature>
</BACSDocument>
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Neil Thompson

ASKER

Many thanks Carl

Tried everything to get it working, cant believe it was so simple (still.. lesson learnt)
I would prefer to use something like:
<xsl:value-of select="/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/Totals/@valueOf"/>

Using //Totals/@numberOf will result in parsing all nodes in the entire XML, and if the XML is long this can have a negative effect on performance. Furthermore, if there is a node Totals somewhere else, with a valueOf attribute, this will be processed as well.
Many thanks R7AF

This is also a great help (superb for my learning) .

Luckily in my case the XML is small and fixed but this will i'm sure help me in the future

Best wishes
Neil
You're welcome. I would use it anyway, because of two reasons:

1) This points direct to the node you want, so it saves processor time.
2) Although it's a longer string, it makes clearer what you want to do, in case you're reviewing the code in the future.