如何使用XSL将原子Feed中的相对链接转换为绝对链接?

时间:2010-06-17 20:08:38

标签: xslt atom-feed

我从Confluence提取Atom Feed。一些链接和图像是相对于域(/),因此当我在不同的网站上使用Feed时,图像和链接会被破坏。

是否可以使用xslt将所有应用程序相对链接转换为绝对链接?有更好的方法吗?

Here's a sample

1 个答案:

答案 0 :(得分:3)

您可以使用/feed/link/@href的值通过在="/节点中查找text()并将其替换为完整路径来为所有相对路径构建绝对路径。

<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>