tags:

views:

33

answers:

1

I'm pulling an Atom feed from Confluence. Some of the links and images are relative to the domain (/), so when I consume the feed on a different website the images and links are broken.

Is it possible to convert all app relative links to absolute with xslt? Is there a better approach?

Here's a sample

+3  A: 

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

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:atom="http://www.w3.org/2005/Atom"&gt;

    <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>
Mads Hansen
@Mads Hansen: Good answer! I think you need to *add one slash* to your **$with**. Also, in this case you could refine the text() pattern with **summary[@type='html']/text()**
Alejandro
@Alejandro - You are right, thanks for the feedback. I have adjusted the stylesheet.
Mads Hansen
That worked, thank you! Our Confluence installation is at www.example.com/confluence, so I had to change the replace param select value from `'="/'` to `'="/confluence/'`.
jrummell