2

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

4

2 回答 2

2

如果你必须使用纯 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 末尾的两个点,则 external-destination 属性将得到一个。

于 2009-12-14T10:55:53.907 回答
0

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

  • 应用另一个 xslt 做你想要的或
  • 使用 SAX 和使用 java regex 函数自己转换它
于 2009-12-14T07:00:55.520 回答