代码之家  ›  专栏  ›  技术社区  ›  jrummell

如何用XSL将Atom提要中的相对链接转换为绝对链接?

  •  4
  • jrummell  · 技术社区  · 15 年前

    我正在从 Confluence . 有些链接和图像是相对于域(/)的,所以当我在其他网站上使用提要时,图像和链接会被破坏。

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

    Here's a sample

    1 回复  |  直到 8 年前
        1
  •  3
  •   Mads Hansen    15 年前

    您可以使用 /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>