Solved

Concatenating multiple occurrences of the same field into one string

Posted on 2014-10-06
8
170 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
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT Display Label Name 1 33
Vector, list Questions R 6 45
ASP and Extracting XML 7 24
How to delete text box content on subform 1 11
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.
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 …
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…

685 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