0

XSL:

<xsl:value-of select="theName" />

Sample Values (Last, First Middle, Title):

- Fitzerald, John K., MBBS
- Keane, Mike

How can I split the theName, taking the samples values above, so it displays like this:

- John K. Fitzerald MBBS
- Mike Keane
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

1

Here's an XSLT 2.0 option using tokenize()...

XML Input

<doc>
    <theName>Fitzerald, John K., MBBS</theName>
    <theName> Fitzerald , John  K. , MBBS  </theName>
    <theName>Keane, Mike</theName>
</doc>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="theName">
    <xsl:variable name="tokens" 
      select="for $token in tokenize(.,',') return normalize-space($token)"/>
    <xsl:copy>
      <xsl:value-of select="($tokens[2],$tokens[1],$tokens[3])" separator=" "/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Output

<doc>
   <theName>John K. Fitzerald MBBS</theName>
   <theName>John K. Fitzerald MBBS</theName>
   <theName>Mike Keane</theName>
</doc>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • I have `` inside a XSLT file already so just use the template? – Si8 Nov 04 '16 at 17:50
  • 1
    @Si8 - The only thing that is different is the context. You can either add the template and do `` instead of `xsl:value-of` or you can just add the `xsl:variable` (change `tokenize(.,',')` to: `tokenize(theName,',')`) and replace the `xsl:value-of` with what I'm using. If that still isn't working, please update your question with an example XML input and output. – Daniel Haley Nov 04 '16 at 17:55
  • My `xml` version at top says 1.0, so I am guessing I was wrong when I said it is 2.0? :/ – Si8 Nov 04 '16 at 18:29
  • @Si8 - Not necessarily. That would be the XML version. The stylesheet version is specified in the `version` attribute of the `xsl:stylesheet` element. However, even if the stylesheet version is 2.0, you still need a 2.0 processor. What XSLT processor are you using? – Daniel Haley Nov 04 '16 at 18:32
  • Nevermind... it is 1.0... `` – Si8 Nov 04 '16 at 18:35
  • I added the template as separate and it is giving me an error... your solution was perfect but unf looks like I can't use it. – Si8 Nov 04 '16 at 18:35