A single example is often not enough to reliably deduce the logic that needs to be applied in order to handle all possible scenarios.
Looking at the example you provided, it seems to me that the real problem here is not how to replace()
, but how to tokenize()
.
Consider the following stylesheet:
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="Telephone">
<PhoneAreaCode>
<xsl:call-template name="tokenize-and-extract">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</PhoneAreaCode>
</xsl:template>
<xsl:template name="tokenize-and-extract">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<xsl:value-of select="substring(translate($token, ' /()-', ''), 1, 3)"/>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:text>|</xsl:text>
<xsl:call-template name="tokenize-and-extract">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Applied to your example input XML, the result will be:
<Job>
<PhoneAreaCode>123|456|234</PhoneAreaCode>
</Job>