[Last Call] Learn how to a build a cloud-first strategyRegister Now


get node values of xml file via xslt

Posted on 2007-08-03
Medium Priority
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="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 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="http://www.w3.org/2001/XMLSchema-instance" 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:Neil Thompson
  • 2
  • 2
LVL 52

Accepted Solution

Carl Tawn earned 2000 total points
ID: 19623708

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

Author Comment

by:Neil Thompson
ID: 19623784
Many thanks Carl

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

Expert Comment

ID: 19623879
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.

Author Comment

by:Neil Thompson
ID: 19623905
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

ID: 19623971
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 learn how to count occurrences of each item in an array.
Suggested Courses

830 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