I have the following xml:
<links url="../servlet/SearchServlet?query=contact&filter=&sort=relevance&sortdir=desc&col=5&startdate=0&enddate=0&xsl=xml&page=">
<link page="date" url="../servlet/SearchServlet?query=contact&filter=&sort=date&col=5&startdate=0&enddate=0&page=1&xsl=xml"/>
I am using this xsl to transform the links:
<xsl:for-each select="//link">
<xsl:choose>
<xsl:when test="@page='next'">
<xsl:text></xsl:text>
<a href="{@url}">>></a>
</xsl:when>
<xsl:when test="@page='prev'">
<a href="{@url}"><<</a>
<xsl:text></xsl:text>
</xsl:when>
<xsl:when test="@page='date'"/>
<xsl:when test="@page='relevance'"/>
<xsl:when test="@page='alpha'"/>
</xsl:choose>
</xsl:for-each>
The tricky part now is I want to replace part of the url
in the link from ../servlet/SearchServlet
to ../search
How do I achieve this? I tried using this template but it replaces the whole url
.
<xsl:template match="link/@url">
<xsl:param name="search" select="'../servlet/SearchServlet'" />
<xsl:param name="replace" select="'../search'" />
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="contains(., $search)">
<xsl:value-of select="concat($replace, substring-after(., $search))" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
call as
<a>
<xsl:apply-templates select="@url" />
<xsl:text><<</xsl:text>
</a>
Note that you could optionally use
<xsl:apply-templates select="@url" />
<xsl:with-param name="search" select="'xyz'" />
<xsl:with-param name="replace" select="'foo'" />
</xsl:apply-templates>
to supply different search/replace values.
This is a simpler and shorter, complete solution, not using any explicit conditional instructions or xsl:attribute
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTarget" select="'../servlet/SearchServlet'"/>
<xsl:param name="pRep" select="'../search'"/>
<xsl:template match="link">
<xsl:variable name="vCUrl" select="concat(@url, $pTarget)"/>
<a url="{substring-before($vCUrl,$pTarget)}{$pRep}{substring-after(@url,$pTarget)}">
<xsl:apply-templates select="@page"/>
</a>
</xsl:template>
<xsl:template match="link/@page[. = 'next']">>></xsl:template>
<xsl:template match="link/@page[. = 'prev']"><<</xsl:template>
<xsl:template match="link[not(@page='next' or @page='prev')]"/>
</xsl:stylesheet>
when applied on the following XML document:
<links>
<link page="prev" url="../servlet/SearchServlet?query=contact&filter=&sort=relevance&sortdir=desc&col=5&startdate=0&enddate=0&xsl=xml&page="/>
<link page="date" url="../servlet/SearchServlet?query=contact&filter=&sort=date&col=5&startdate=0&enddate=0&page=1&xsl=xml"/>
<link page="next" url="../servlet/SearchServlet?query=contact&filter=&sort=date&col=5&startdate=0&enddate=0&page=1&xsl=xml"/>
</links>
the wanted, correct result is produced:
<a url="../search?query=contact&filter=&sort=relevance&sortdir=desc&col=5&startdate=0&enddate=0&xsl=xml&page="><<</a>
<a url="../search?query=contact&filter=&sort=date&col=5&startdate=0&enddate=0&page=1&xsl=xml">>></a>
Explanation:
Appropriate use of AVT (Attribute Value Templates)
Appropriate use of pattern matching and templates.
Adding a sentinel to a string so that conditional processing becomes unnecessary.