Use XSLT replace function with external CSV look up table



I currently have an XSLT that searches across multiple strings using XPath in order to find certain words and if they match replace with the alternate word. This is highlighted with the 'old' and 'new' tags at the beginning of my current XSLT.


XSL EXAMPLE



<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>

<my:reps>
<rep>
<old>neighbor</old>
<new>neighbour</new>
</rep>
</my:reps>

<xsl:variable name="vReps" select="document('')/*/my:reps/*"/>

<xsl:output omit-xml-declaration="no" indent="yes"/>

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

<xsl:template match="series_synopsis_long/text() | series_synopsis_medium/text() | series_synopsis_short/text() | season_synopsis_short/text() | season_synopsis_medium/text() | season_synopsis_long/text() | episode_synopsis_short/text() | episode_synopsis_medium/text() | episode_synopsis_long/text() | episode_keywords/text() | season_keywords/text()" name="replace">
<xsl:param name="pText" select="."/>

<xsl:choose>
<xsl:when test="not($vReps/old[contains($pText, .)])">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="multiReplace">
<xsl:with-param name="pText" select="$pText"/>
<xsl:with-param name="pReps"
select="$vReps[contains($pText, old)]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="multiReplace">
<xsl:param name="pText"/>
<xsl:param name="pReps"/>

<xsl:choose>
<xsl:when test="$pReps">
<xsl:variable name="vRepResult">
<xsl:call-template name="singleReplace">
<xsl:with-param name="pText" select="$pText"/>
<xsl:with-param name="pOld" select="$pReps[1]/old"/>
<xsl:with-param name="pNew" select="$pReps[1]/new"/>
</xsl:call-template>
</xsl:variable>

<xsl:call-template name="multiReplace">
<xsl:with-param name="pText" select="$vRepResult"/>
<xsl:with-param name="pReps" select="$pReps[position() >1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pText"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="singleReplace">
<xsl:param name="pText"/>
<xsl:param name="pOld"/>
<xsl:param name="pNew"/>

<xsl:if test="$pText">
<xsl:choose>
<xsl:when test="not(contains($pText, $pOld))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($pText, $pOld)"/>
<xsl:value-of select="$pNew"/>
<xsl:call-template name="singleReplace">
<xsl:with-param name="pText" select="substring-after($pText, $pOld)"/>
<xsl:with-param name="pOld" select="$pOld"/>
<xsl:with-param name="pNew" select="$pNew"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


I would like to be able to manage an external look up table externally in a csv document (or other suitable format) to keep the code removed from a table that will be updated regularly by many users. The idea is that if values are listed in columns with headings of 'old' and 'new' that i should be able to link these as variables into my existing xslt sheet...


Any guidance on this would be appreciated as so far csv is proving more problematic to navigate with xslt as first thought! My thoughts are do i need to use a key() function perhaps or to convert the csv to an xml first and then process the xml as my external look up every time i need to call a transform?


No comments:

Post a Comment