我想将随机文本中任何基于 http/s 的 url 转换为使用 xsl-fo 自动标记,其中随机文本可能包含一个或多个基于 http/s 的 url。
因此,http/s url 不是属性的一部分,也不是节点的唯一内容,而是节点内文本的一部分。
例如:来源
<misc>
<comment>Yada..yada..yadda, see http://www.abc.com.
Bla..bla..bla.. http://www.xyz.com</comment>
</misc>
将被转换成类似的东西:
<fo:block>
Yada..yada..yadda, see <fo:basic-link external-destination="http://www.abc.com">http://www.abc.com</fo:basic-link>.
Bla..bla..bla.. <fo:basic-link external-destination="http://www.xyz.com">http://www.xyz.com</fo:basic-link>
<fo:/block>
我们使用的库是 Apache FOP 和 Xalan-J。
最佳答案
如果您必须使用纯 XSLT 方法,您可以使用这个:
<xsl:template match="comment">
<fo:block>
<xsl:call-template name="dig-links">
<xsl:with-param name="content" select="."/>
</xsl:call-template>
</fo:block>
</xsl:template>
<xsl:template name="dig-links">
<xsl:param name="content" />
<xsl:choose>
<xsl:when test="contains($content, 'http://')">
<xsl:value-of select="substring-before($content, 'http://')"/>
<xsl:variable name="url" select="concat('http://', substring-before(substring-after(concat($content, ' '), 'http://'), ' '))"/>
<fo:basic-link>
<xsl:attribute name="external-destination">
<xsl:choose>
<xsl:when test="substring($url, string-length($url), 1) = '.'">
<xsl:value-of select="substring($url, 1, string-length($url)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$url"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$url"/>
</fo:basic-link>
<xsl:call-template name="dig-links">
<xsl:with-param name="content" select="substring-after($content, $url)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$content"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
但这不是一个完美的解决方案,因此如果您有拼写错误,例如 url 末尾有两个点,外部目标属性将得到一个。
关于java - 使用xslt将xml转换为xsl-fo时如何创建超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1898611/