Solved

Concatenating multiple occurrences of the same field into one string

Posted on 2014-10-06
8
181 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

688 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