Solved

Concatenating multiple occurrences of the same field into one string

Posted on 2014-10-06
8
175 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 400 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
incorrect syntax near the order by 10 76
C# XML Get Values 4 46
Java List 4 70
.XSN saves to .XML 3 22
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

731 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