Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 10th, 2010, 11:23 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Recursive loop to concatenate values

version 1.0 (using Altova xml spy)

Hi,

I have an xml sheet that contains several repeating nodes that contain different values. I am using a recusive template within my xslt sheet but can't seem to solve my issue. I have used a similar recursive template to evaluate other xml sheets where the functionality works!!!

Basically, in xml there is an element named 'Narrative'
if
its attributes Type='990' and Sequence='2'
OR
its attributes Type='991' and Sequence='2'
I want to concatenate the values (in this instance the result would be 08166)

The XML is
Code:
 
<?xml version="1.0" encoding="UTF-8"?>
<F4FDeliveryNotification xmlns:fo="http://www.w3.org/1999/XSL/Format" DeliveryNotificationType="Goods Received Note" DocumentType="New" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<F4FDocumentHeader>
....
</F4FDocumentHeader>
<DeliveryNotificationHeader>
<DeliveryReference ReferenceType="Order Number" AssignedBy="Buyer">CB10</DeliveryReference>
<DeliveryReference ReferenceType="Contract Number" AssignedBy="Buyer">D10</DeliveryReference>
<DeliveryReference AssignedBy="Supplier" ReferenceType="Delivery Note Number">D1</DeliveryReference>
<DeliveryDate DateTimeType="Document Created">20090609 09:46:38</DeliveryDate>
<DeliveryDate DateTimeType="Delivery Date">20090520 10:21:01</DeliveryDate>
<TradingPartner PartnerType="Supplier">
<PartnerID PartnerIDType="Assigned by F4F">ME</PartnerID>
</TradingPartner>
<TradingPartner PartnerType="Buyer">
<PartnerID PartnerIDType="Assigned by F4F">YOU</PartnerID>
<Address AddressType="Sold To">
<AddressID AddressIDType="Assigned by Supplier">999</AddressID>
<CompanyName>Acme</CompanyName>
<StreetAddress>War Road</StreetAddress>
<StreetAddress>Blah</StreetAddress>
<CityName>LEEDS</CityName>
<CountyDistrictName>West Yorkshire</CountyDistrictName>
<PostalCode>LL</PostalCode>
</Address>
</TradingPartner>
<Narrative Type="990" Sequence="1">08</Narrative>
<Narrative Type="990" Sequence="2">08</Narrative>
<Narrative Type="991" Sequence="1">164</Narrative>
<Narrative Type="991" Sequence="2">166</Narrative>
</DeliveryNotificationHeader>
<TransportDetails>
....
</TransportDetails>
<DeliveryLine LineNumber="1">
....
</DeliveryLine>
<DeliveryControlTotals>
....
</DeliveryControlTotals>
</F4FDeliveryNotification>
The XSL is
Code:
 
xsl:call-template name="QtyCalc">
<xsl:with-param name="parIncrement" select="1"/>
<xsl:with-param name="parCount" select="''"/>
</xsl:call-template>

....
 
<xsl:template name="QtyCalc">
<xsl:param name="parIncrement" select="1"/>
<xsl:param name="parCount" select="''"/>
<xsl:for-each select=".//DeliveryNotificationHeader/Narrative[@node()]">
<!--<xsl:if test="position()=$parIncrement">-->
<xsl:choose>
<xsl:when test="./@Type='990' and ./@Sequence='2'">
<xsl:call-template name="QtyCalc">
<xsl:with-param name="parIncrement" select="$parIncrement + 1"/>
<xsl:with-param name="parCount" select="concat($parCount, .)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="./@Type='991' and ./@Sequence='2'">
<xsl:call-template name="QtyCalc">
<xsl:with-param name="parIncrement" select="$parIncrement + 1"/>
<xsl:with-param name="parCount" select="concat($parCount, .)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="QtyCalc">
<xsl:with-param name="parIncrement" select="$parIncrement + 1"/>
<xsl:with-param name="parCount" select="$parCount"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose><xsl:if test="position()=last()">
<VCTTicketNumber>
<xsl:value-of select="$parCount"/>
</VCTTicketNumber>
</xsl:if>
<!--</xsl:if>-->
</xsl:for-each>
</xsl:template>
In this example, the $parIncrement does NOT increment so I commented-out the <xsl:if test="position()=$parIncrement"> line.

The $parCount variable is populated with the current node value but I can't use it to concatenate and the final output is
<VCTTicketNumber xmlns=""/>

whereas it should read
<VCTTicketNumber>08166</VCTTicketNumber>

Please can someone help?

Thanks in advance,
__________________
Neal

A Northern Soul

Last edited by Neal; June 10th, 2010 at 11:26 AM..
 
Old June 10th, 2010, 11:34 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You appear to be overthinking the problem. The following outputs what you have asked for without any recurring templates.

Code:
   <xsl:template match="/">
    <VCTTicketNumber>
      <xsl:for-each select=".//DeliveryNotificationHeader/Narrative[@Type='990' or @Type='991'][@Sequence='2']"><xsl:value-of select="."/></xsl:for-each>
    </VCTTicketNumber>
   </xsl:template>
In XSLT 2.0 (which the latest version of Altova XML Spy uses I believe) this would be even easier:

Code:
   <xsl:template match="/">
    <VCTTicketNumber>
      <xsl:value-of select=".//DeliveryNotificationHeader/Narrative[@Type='990' or @Type='991'][@Sequence='2']" separator=""/>
    </VCTTicketNumber>
   </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
Neal (June 10th, 2010)
 
Old June 10th, 2010, 12:13 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

You're right, I was.

Thanks!!
__________________
Neal

A Northern Soul
 
Old June 10th, 2010, 01:03 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Inside a single call of the recursive template, you have an xsl:for-each which is processing all the Narrative elements in turn. For all of them except the last, you call a template (it happens to be a recursive call) but do nothing with the result of the call. When you process the last one, you output the value of one of the parameters that was supplied in the initial call - which happens to be an empty string.

I don't think you need recursion here at all: just use the for-each, and output the value if it's selected; the concatenation happens automatically.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to concatenate two values into one field haixia XSLT 3 November 7th, 2016 02:33 PM
Recursive Loop in XSLT file dhrumil XSLT 36 April 13th, 2010 04:43 AM
XSLT: Need to concatenate strings in loop and hold them for later use sandeepbhutani304 XSLT 3 January 9th, 2009 11:31 AM
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.