-1

I have an XML with some tags like those:

<firstName>George</firstName>
<middleName>M</middleName>
<lastName>Stuard</lastName>

(and many other taks), and I would need in my XSL to concat the three names to have an

<fullName>George M Stuard</fullName>

in the output XML. I found some examples but none of them works, or better said, I don't know how to make them works.

I tried with something like this:

<fullName>
  <xsl:template name="ConcatMyXML" match="name">
    <xsl:variable name="MyConcatVar">
     <xsl:for-each select="givenNames/lMiddleName/familyName">
        <xsl:value-of select="./text()"/>
     </xsl:for-each>
    </xsl:variable>
  </xsl:template>
 <xsl:value-of select="$MyConcatVar"/>
</fullName>

Can someone give me a simple solution? Thanks!

WDrgn
  • 521
  • 10
  • 29
  • well, I saw that one but I couldn't find how to apply that in my case... – WDrgn Nov 28 '18 at 08:23
  • 1
    Can you show a little bit more of your XML, please? What is the parent node of `firstName`, `middleName` and `lastName`? Are there are other child nodes within this parent node, or is it just this three? Thanks! – Tim C Nov 28 '18 at 08:24
  • @WDrgn "*I found some examples but none of them works*" This is a prime example of 'voodoo programming'. If you had spent an hour with a basic XSLT tutorial, you would have known how to do this. – michael.hor257k Nov 28 '18 at 08:42
  • @michael.hor257k, in a company where you have to deal with more issues than you can do, sometimes, you just have to find an quick answer, not having the time to learn XSLT stuff because you deal with Java or I don't know what other language, but you have to deal with "small issues" like this... So, 'voodoo programming' is a way of doing things sometimes (when it's not about your main programming language) – WDrgn Nov 28 '18 at 10:24

1 Answers1

1

You can achieve it by following code as per your given information:

<fullName>
    <xsl:for-each select="givenNames/lMiddleName/familyName">
        <xsl:value-of select="concat(firstName,' ',middleName,' ',lastName)" />
    </xsl:for-each>
</fullName>

And for this (and many other taks), if you need to use the variable:

<fullName>
    <xsl:variable name="MyConcatVar">
        <xsl:for-each select="givenNames/lMiddleName/familyName">
            <xsl:value-of select="concat(firstName,' ',middleName,' ',lastName)" />
        </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="$MyConcatVar" />
</fullName>

Change the XPATH in for-each based on your node tree.

Vebbie
  • 1,669
  • 2
  • 12
  • 18