?
Solved

Concatenating multiple occurrences of the same field into one string

Posted on 2014-10-06
8
Medium Priority
?
190 Views
Last Modified: 2014-10-13
Hi

I have to concatenate the multiple occurrences of the same field as follows, FYI, I am using xslt 1.0

Input code fragment
 <E1EDKT1 SEGMENT="1">
         <TDID>ZTDT</TDID>
         <E1EDKT2 SEGMENT="1"/>
            <TDLINE>ABC</TDLINE>
         </E1EDKT2>
        <E1EDKT2 SEGMENT="1"/>
            <TDLINE>DEF</TDLINE>
         </E1EDKT2>
     </E1EDKT1>

Open in new window


Output desired

<Reference> ABCDEF </Reference>

Open in new window


Code  that I am trying to apply by using string-join function
<Reference>
			    <xsl:when test="normalize-space(E1EDKT1/TDID) = 'ZTDT'">
				  <xsl:value-of select="fn:string-join(/E1EDKT2/TDLINE, ' ')" />
				</xsl:when>
  </Reference>

Open in new window

0
Comment
Question by:kalyangkm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 

Author Comment

by:kalyangkm
ID: 40365193
As String-join is not supported in XSLT 1.0 I am trying in a different way but having trouble achieving the concatenation. Please advice.

<xsl:for-each select="E1EDKT1">
			      <xsl:choose>
			         <xsl:when test="normalize-space(TDID) = 'F01'">
				   <xsl:for-each select="E1EDKT2/TDLINE">
					<Reference>
				            <xsl:value-of select="." />              
                                          </Reference>
				    </xsl:for-each>
				   </xsl:when>
				  </xsl:choose>
				 
		 </xsl:for-each> 

Open in new window

0
 

Assisted Solution

by:kalyangkm
kalyangkm earned 0 total points
ID: 40365234
I got the target xml with slight modification

<xsl:for-each select="E1EDKT1">
								<xsl:choose>
									<xsl:when test="normalize-space(TDID) = 'F01'">
										<Reference>
											<xsl:for-each select="E1EDKT2/TDLINE">
												<xsl:value-of select="."/>
											</xsl:for-each>
										</Reference>
									</xsl:when>
								</xsl:choose>
							</xsl:for-each>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1600 total points
ID: 40365695
<Reference>
  <xsl:for-each select="E1EDKT2/TDLINE">
    <xsl:value-of select="."/>
  </xsl:for-each>
</Reference>

Open in new window


Is better done like this

<Reference>
   <xsl:value-of select="E1EDKT2/TDLINE"/>
</Reference>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40365699
value-of does concatenate all the string nodes found

you can also use concat()
<Reference>
   <xsl:value-of select="concat(E1EDKT2[1]/TDLINE, ' ', E1EDKT2[2]/TDLINE)"/>
</Reference>

If you want a space in between (and you know there is only two)

Depending on the exact requirements there is a lot more you can do
0
 
LVL 29

Assisted Solution

by:Pravin Asar
Pravin Asar earned 400 total points
ID: 40367599
This should work for you.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text"/>
<xsl:template match="/E1EDKT1">
<xsl:for-each select="E1EDKT2/TDLINE">
<xsl:value-of select="concat(., '=')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

To test, I used following XML data

<?xml version="1.0" encoding="UTF-8"?>
<E1EDKT1 SEGMENT="1">
      <TDID>ZTDT</TDID>
      <E1EDKT2 SEGMENT="1">
            <TDLINE>ABC</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
            <TDLINE>DEF</TDLINE>
      </E1EDKT2>
</E1EDKT1>
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 1600 total points
ID: 40367728
some comments with @pravinasar's code

I understand in the real XML, E1EDKT1 is not the root,
so this template <xsl:template match="/E1EDKT1"> will never fire
you should remove the '/' to make it work

this code is adding a '=' between all te nodes
if that would be the requirement, here is how to avoid the trailing extra '='
<xsl:for-each select="E1EDKT2/TDLINE">
   <xsl:if test="not(position() = 1)">
      <xsl:text>=</xsl:text>
</xsl:if>
    <xsl:value-of select="."/>
 </xsl:for-each>

Open in new window

instead of
<xsl:for-each select="E1EDKT2/TDLINE">
 <xsl:value-of select="concat(., '=')"/>
 </xsl:for-each>

Open in new window

0
 

Author Closing Comment

by:kalyangkm
ID: 40376639
Because it is the correct solution
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

764 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