Java 如何在使用xlst将xml转换为xsl fo时创建超链接?

Java 如何在使用xlst将xml转换为xsl fo时创建超链接?,java,xml,xslt,xalan,Java,Xml,Xslt,Xalan,我想将随机文本中任何基于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> &l

我想将随机文本中任何基于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>

雅达..雅达..雅达,看到了吗http://www.abc.com. 
布拉..布拉..布拉。。http://www.xyz.com
将被转换为以下内容:

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

雅达..雅达..雅达,看到了吗http://www.abc.com.
布拉..布拉..布拉。。http://www.xyz.com

我们使用的库是ApacheFop和Xalan-J。

我不知道xsl:fo是什么意思,但是如果您可以访问实际执行转换的xslt文件,您可以使用前面提到的xml字符串函数自己进行转换。如果您只能访问转换后的输出,您仍然可以

  • 应用另一个xslt来实现您想要的功能,或者
  • 使用SAX和java正则表达式函数自己对其进行转换

如果必须使用纯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末尾有两个点,则外部目标属性将得到一个。

您使用的是XSLT版本1还是2?后者具有XPath2.0中更好的字符串操作函数,包括正则表达式匹配。