0

I have an source schema with a node forenames (containing forename + ' ' + middlename), which I need to separate out the middle name in the destination schema so this goes out to OtherNames.

I currently have the following xslt template:

<xsl:template name="StringSplit">
<xsl:param name="valFirstnames" />

<xsl:choose>
  <xsl:when test="contains($valFirstnames, ' ')">
      <xsl:call-template name="StringSplit">
        <xsl:with-param name="valFirstnames" select="substring-after($valFirstnames, ' ')" />
      </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
     <OtherFirstNames><xsl:value-of select="$valFirstnames" /></OtherFirstNames>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

Current output for this xslt template is writing out the Middlename twice rather than once:

<OtherFirstName>Middlename</OtherFirstName>
<OtherFirstName>Middlename</OtherFirstName>

Expected:

<OtherFirstName>Middlename</OtherFirstName>

Sample Input

<Data>
  <SubjectName>
    <forenames>first middle</forenames>
  </SubjectName>
  <SubjectPartner>
    <forenames>first middle</forenames>
    <Otherforenames>first middle</Otherforenames>
  </SubjectPartner>
  <etc./>
</Data>

Sample Output

<Data>
  <SubjectName>
    <firstname>first</firstname>
    <OtherFirstName>middle</OtherFirstname>
  </SubjectName>
  <SubjectPartner>
    <firstname>first</firstname>
    <OtherFirstName>middle</OtherFirstName>
    <OtherFirstName>middle</OtherFirstName>
  </SubjectPartner>
  <etc./>
</Data>

I'm looking at correcting the current xslt and updating to incorporate other partner elements that have the same child elements forenames.

Tim C
  • 70,053
  • 14
  • 74
  • 93
PuffTMD
  • 63
  • 9
  • 1
    Are you sure those input/output samples are correct? – michael.hor257k Jun 04 '14 at 14:45
  • Is there a reson this has to be Xslt? You'd much better string manipulating in C# in either a Script Functoid or Extension Method. – Johns-305 Jun 04 '14 at 15:08
  • "*Current output for this xslt template is writing out the Middlename twice rather than once*" No, I don't think it does that. Post a **complete** code example so that we can try and reproduce your result. – michael.hor257k Jun 04 '14 at 16:29
  • @boatseller no reason in particular, but since the map uses that I thought it would be good to implement this in xslt since i believe it could be done this way. – PuffTMD Jun 04 '14 at 22:07
  • @michael.hor257k thanks for your comments, the issues seems to duplicate the firstname element if there is no middle name (firstname) but if there is a firstname and middle name the xslt appears to work. Is the otherforename node causing the issue for you? – PuffTMD Jun 04 '14 at 22:10
  • True, but the point of the Mapper is to abstract the Xslt ;) My advice, and certainly what I would do, is build a regulate BizTalk Map and use Scripting Functoids to parse out the data. – Johns-305 Jun 04 '14 at 22:16
  • If you don't post a **complete** code that can be run directly without modification, then this is not going anywhere. – michael.hor257k Jun 04 '14 at 22:56

1 Answers1

1

Your examples do not make much sense to me. I am guessing you want to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="forenames">
    <firstname><xsl:value-of select="substring-before(concat(., ' '), ' ')"/></firstname>
    <xsl:if test="contains(., ' ')">
        <OtherFirstName><xsl:value-of select="substring-after(., ' ')"/></OtherFirstName>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Note that this assumes a person has at most two forenames. When applied to the following test input:

<Data>
    <SubjectName>
        <forenames>Alan Benjamin</forenames>
        <surname>Adams</surname>
    </SubjectName>
    <SubjectPartner>
        <forenames>Cecily Diana</forenames>
        <surname>Crown</surname>
    </SubjectPartner>
    <Single>
        <forenames>Eve</forenames>
        <surname>Evans</surname>
    </Single>
    <Triple>
        <forenames>Frank George Herbert</forenames>
        <surname>Forrester</surname>
    </Triple>
</Data>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
   <SubjectName>
      <firstname>Alan</firstname>
      <OtherFirstName>Benjamin</OtherFirstName>
      <surname>Adams</surname>
   </SubjectName>
   <SubjectPartner>
      <firstname>Cecily</firstname>
      <OtherFirstName>Diana</OtherFirstName>
      <surname>Crown</surname>
   </SubjectPartner>
   <Single>
      <firstname>Eve</firstname>
      <surname>Evans</surname>
   </Single>
   <Triple>
      <firstname>Frank</firstname>
      <OtherFirstName>George Herbert</OtherFirstName>
      <surname>Forrester</surname>
   </Triple>
</Data>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51