I've come up with a hackish approach to get my XSL function to only return the first 2 initials of a person. I think there should be a more efficient way to do this though. The replace
specifically is what I think I should be able to get rid of, looking through docs though I can't figure out how to correctly iterate with the split.
So 5 examples of my XML:
<name>
<surname>Test I</surname>
<given-names>Chris</given-names>
</name>
<name>
<surname>Test II</surname>
<given-names>Chris Chris</given-names>
</name>
<name>
<surname>Test III</surname>
<given-names>Chris Eat Meat</given-names>
</name>
<name>
<surname>Test IV</surname>
<given-names>Chris-Eat Meat</given-names>
</name>
<name>
<surname>Test V</surname>
<given-names>Chris-Eat-Meat</given-names>
</name>
My function call:
<xsl:value-of select="pull_it:get_initials(name/given-names/text())"/> <!-- name/given-names -->
and my inefficient XSL function:
<xsl:function name="pull_it:get_initials">
<xsl:param name="names"/>
<xsl:value-of select="
replace(string-join(
for $token in tokenize($names,'(-|\s+)')[string-length(.) > 0]
return concat(substring($token,1,1),'.'),
''), '(\w\.\w?\.?).*', '$1')
"/>
</xsl:function>
So with the first two XML blocks the expected output/return is:
C.
and
C. C.
The remaining three should return (as expected, no M.
because we only want the first 2 initials)
C. E.
but I thought there would be a way to stop the iteration. For example my PHP just has:
$abbreviated_firstnames = array();
$firstnames = mb_split('(\s+|-)', html_entity_decode($this->firstname,ENT_QUOTES,'UTF-8'));
$intial_count = 0;
foreach ($firstnames as $firstname) {
$intial_count++;
$firstinit = mb_substr($firstname,0,1,'UTF-8');
if ($firstinit) {
$abbreviated_firstnames[] = $firstinit . '.';
if($intial_count >= 2) {
break; // <---- we got 2 matches stop NOW
}
}
}
return implode(' ',$abbreviated_firstnames);