Solved

Concatenating multiple occurrences of the same field into one string

Posted on 2014-10-06
8
153 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
  • 3
  • 3
8 Comments
 

Author Comment

by:kalyangkm
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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 28

Assisted Solution

by:Pravin Asar
Pravin Asar earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Because it is the correct solution
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now