Replace part of string in XML with XSL
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 convert 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>
Now the tricky part is to replace the part url
in the link from ../servlet/SearchServlet
to../search
How can I achieve this? I tried with this pattern but it replaces everything url
.
source to share
<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
<a>
<xsl:apply-templates select="@url" />
<xsl:text><<</xsl:text>
</a>
Please note that you can 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.
source to share
This is a simpler, shorter, complete solution that does not use explicit conditionals, 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 to 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>
required, the correct result is obtained :
<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 Patterns )
-
Appropriate use of pattern matching and pattern matching.
-
Adding a watchdog to the string so that conditional processing is unnecessary.
source to share