How to convert relative links in Atom feed to absolute with XSL?

I am pulling an Atom feed from Confluence . Some links and images belong to the domain (/), so when I consume a feed on another website, images and links do not work.

Is it possible to convert all relative application links to absolute using xslt? Is there a better approach?

Here is an example

+4
source share
1 answer

You can use the value /feed/link/@href to build an absolute path for all relative paths by looking for ="/ in the text() nodes and replacing it with the full path.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom"> <xsl:template match="atom:summary[@type='html']/text()" > <xsl:call-template name="replace-string"> <xsl:with-param name="text" select="." /> <xsl:with-param name="replace" select="'=&quot;/'" /> <xsl:with-param name="with" select="concat('=&quot;', /atom:feed/atom:link/@href, '/')"/> </xsl:call-template> </xsl:template> <!--recursive template that replaces string values --> <xsl:template name="replace-string"> <xsl:param name="text"/> <xsl:param name="replace"/> <xsl:param name="with"/> <xsl:choose> <xsl:when test="contains($text,$replace)"> <xsl:value-of select="substring-before($text,$replace)"/> <xsl:value-of select="$with"/> <xsl:call-template name="replace-string"> <xsl:with-param name="text" select="substring-after($text,$replace)"/> <xsl:with-param name="replace" select="$replace"/> <xsl:with-param name="with" select="$with"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!--identity template --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 
+3
source

Source: https://habr.com/ru/post/1313111/


All Articles