Neil Thompson
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="//ReturnedDebitIte m"/>
<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(PayerAcc ount/@sort Code,'-',' ')" />
<xsl:value-of select="PayerAccount/@numb er" />
<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-le ngth($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:t ext>
<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:noNamespaceSchemaLocat ion="newba cs-advices .xsd">
<Data>
<ARUDD>
<Header reportType="abc123" adviceNumber="123" currentProcessingDate="200 7-07-27">< /Header>
<AddresseeInformation name="test"></AddresseeInf ormation>
<ServiceLicenseInformation userName="test" userNumber="123"></Service LicenseInf ormation>
<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="20 07-07-25" valueOf="25.00" currency="GBP"><PayerAccou nt number="12345678" ref="P00531 " name="asdfg & dsfsdf" sortCode="01-01-01" bankName="My Bank 1" branchName="HIGH STREET"></PayerAccount></R eturnedDeb itItem>
<ReturnedDebitItem ref="PAR123 " transCode="77" returnCode="444" returnDescription="asdasd" originalProcessingDate="20 07-07-25" valueOf="75.00" currency="GBP"><PayerAccou nt number="87654321" ref="P00674 " name="zxccxz & dsfsdf" sortCode="02-02-02" bankName="My Bank 2" branchName="LOW STREET"></PayerAccount></R eturnedDeb itItem>
<Totals numberOf="2" valueOf="100..00" currency="GBP"></Totals>
</OriginatingAccountRecord >
</OriginatingAccountRecord s>
</Advice>
</ARUDD>
</Data>
<SignatureMethod></Signatu reMethod>
<Signature></Signature>
</BACSDocument>
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="//ReturnedDebitIte
<xsl:call-template name="footer"/>
</xsl:template>
<xsl:template name="header">
<xsl:text>header
</xsl:template>
<xsl:template match="ReturnedDebitItem">
<xsl:text>aaaaaaaaaaaaaa</
<xsl:value-of select="translate(PayerAcc
<xsl:value-of select="PayerAccount/@numb
<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,
<xsl:value-of select="$vo"/>
</xsl:variable>
<xsl:value-of select="$res"/>
<xsl:text>ABC DIRECT </xsl:text>
<xsl:text>bbbbbbbbb</xsl:t
<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:noNamespaceSchemaLocat
<Data>
<ARUDD>
<Header reportType="abc123" adviceNumber="123" currentProcessingDate="200
<AddresseeInformation name="test"></AddresseeInf
<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"
<ReturnedDebitItem ref="PAR123 " transCode="77" returnCode="444" returnDescription="asdasd"
<Totals numberOf="2" valueOf="100..00" currency="GBP"></Totals>
</OriginatingAccountRecord
</OriginatingAccountRecord
</Advice>
</ARUDD>
</Data>
<SignatureMethod></Signatu
<Signature></Signature>
</BACSDocument>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would prefer to use something like:
<xsl:value-of select="/BACSDocument/Data /ARUDD/Adv ice/Origin atingAccou ntRecords/ Originatin gAccountRe cord/Total s/@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.
<xsl:value-of select="/BACSDocument/Data
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.
ASKER
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
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.
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.
ASKER
Tried everything to get it working, cant believe it was so simple (still.. lesson learnt)