Jump to content

How to do loop and concatenate three fields


EENew02

Recommended Posts

Hi,I am trying to do my transformation but I am having a hard time checking for one field but being send multiple times.So when sending an order, I only have one order but within the order I have three service_id, it would look like this in my input fileInput file<service_id>123<service_id><service_id>456<service_id><service_id>789<service_id>The expected Ouput need to look like this<ServiceNumber>123, 456, 789</ServiceNumber>How do I take the above values and concatenate into one field?If I do the below code it will not work because how does it know which service_id it will concat together, since the field names are the same.<ServiceNumber> <xsl:value-of select="concat(service_id, ',', service_id, ',', service_id' "/><ServiceNumber>PLEASE PLEASE HELP...I am so frustrated and stuck.thanks in advance

Link to comment
Share on other sites

Simply use the position() to move from one element to another:

<ServiceNumber><xsl:value-of select="concat(service_id[position()=1], ',' ,service_id[position()=2], ',' ,service_id[position()=3]"/><ServiceNumber>

It's a bit trickier if the number of service_id elements is unknown though. Is that the case here, or does this work for you?

Link to comment
Share on other sites

Simply use the position() to move from one element to another:
<ServiceNumber><xsl:value-of select="concat(service_id[position()=1], ',' ,service_id[position()=2], ',' ,service_id[position()=3]"/><ServiceNumber>

It's a bit trickier if the number of service_id elements is unknown though. Is that the case here, or does this work for you?

Hi boen_robot,Thank you for your reply. Yeah it's a little trickier in this case, the service_id elements are unknown. I was just given an example. The service_id elements could be anything. That's why I am not sure how to do it. I am going to try what you have provided. In the example I gave three service_id but assuming I don't know how many service_id I will have, sometimes I can have 1 or sometimes I can have 8 service_id. Somehow I think I have to create a loop or something like that but I am not sure how to. Can you please help me see if you have any other options that I can resolve this issue. I greatly appreciate it. Thanks
Link to comment
Share on other sites

As a matter of fact, I figured the solution shortly after I posted the first one, but I was too busy to post it :) .Simply define a loop as normal. The output is converted to a single text node, so it's much like a concatenation:

<ServiceNumber><xsl:for-each select="service_id"><xsl:value-of select="."/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each><ServiceNumber>

If you need to re-use this value in another element, then you could enclose everything up to the <ServiceNumber> element in a template that you would call, or a variable. Each method has it's pros and cons, depending on the situation.

Edited by boen_robot
Link to comment
Share on other sites

As a matter of fact, I figured the solution shortly after I posted the first one, but I was too busy to post it :) .Simply define a loop as normal. The output is converted to a single text node, so it's much like a concatenation:
<ServiceNumber><xsl:variable name="ServiceNumber"><xsl:for-each select="service_id"><xsl:value-of select="."/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each><ServiceNumber>

If you need to re-use this value in another element, then you could enclose everything up to the <ServiceNumber> element in a template that you would call, or a variable. Each method has it's pros and cons, depending on the situation.

Thank you so much for taken your time to answer my question. I will definitely try this out and I will let you know. Thank you thank you!!!! :)
Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...