Xslt 如何使用XSL将atom提要中的相对链接转换为绝对链接?
我正在从中提取一个原子源。有些链接和图像是与域(/)相关的,因此当我在其他网站上使用提要时,图像和链接会被破坏 是否可以使用xslt将所有应用程序相对链接转换为绝对链接?有更好的方法吗Xslt 如何使用XSL将atom提要中的相对链接转换为绝对链接?,xslt,atom-feed,Xslt,Atom Feed,我正在从中提取一个原子源。有些链接和图像是与域(/)相关的,因此当我在其他网站上使用提要时,图像和链接会被破坏 是否可以使用xslt将所有应用程序相对链接转换为绝对链接?有更好的方法吗 您可以使用/feed/link/@href的值为所有相对路径构建一个绝对路径,方法是在text()节点中查找=“/,并将其替换为完整路径 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
您可以使用
/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="'="/'" />
<xsl:with-param name="with" select="concat('="', /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>
是的,您可以将链接从相对转换为绝对。通过atom示例,我们可以准确地回答如何转换。我已经向示例提要添加了一个链接。回答得好!我认为您需要在$With中添加一个斜杠。此外,在这种情况下,您可以使用summary[@type='html']/text()优化text()模式@Alejandro-你说得对,谢谢你的反馈。我已经调整了样式表。这很有效,谢谢你!我们的Confluence安装在www.example.com/Confluence,所以我不得不将replace param select值从'=“/”
更改为'=“/Confluence/”
。