get node values of xml file via xslt

Posted on 2007-08-03
Last Modified: 2013-11-18

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!


<?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl=""
      xmlns: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 name="header">    

<xsl:template match="ReturnedDebitItem">
    <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:value-of select="$res"/>        
    <xsl:text>ABC DIRECT </xsl:text>
    <xsl:value-of select="PayerAccount/@ref" />
    <xsl:value-of select="PayerAccount/@name" />    
<xsl:template name="footer">    
    <xsl:text>footer1 </xsl:text>  
    ######## need valueOf from Totals node here #########
    <xsl:text> footer2</xsl:text>        

<?xml version="1.0" encoding="UTF-8"?>
<BACSDocument xmlns:xsi="" xsi:noNamespaceSchemaLocation="newbacs-advices.xsd">
      <Header reportType="abc123" adviceNumber="123" currentProcessingDate="2007-07-27"></Header>
      <AddresseeInformation name="test"></AddresseeInformation>
      <ServiceLicenseInformation userName="test" userNumber="123"></ServiceLicenseInformation>
            <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>
Question by:NeilT
    LVL 52

    Accepted Solution


    <xsl:template name="footer">    
        <xsl:text>footer1 </xsl:text>  
        <xsl:value-of select="//Totals/@numberOf" />
        <xsl:value-of select="//Totals/@valueOf" />
        <xsl:text> footer2</xsl:text>        
    LVL 3

    Author Comment

    Many thanks Carl

    Tried everything to get it working, cant believe it was so simple (still.. lesson learnt)
    LVL 13

    Expert Comment

    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.
    LVL 3

    Author Comment

    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
    LVL 13

    Expert Comment

    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.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now