【问题标题】:create hyper links dynamically when transforming xml to xsl-fo using xlst?使用 xslt 将 xml 转换为 xsl-fo 时动态创建超链接?
【发布时间】:2014-01-24 09:12:27
【问题描述】:

我想使用我的 xsl 文件在我的 PDF 报告中创建一个标题。如果源文件包含超链接,则应将其呈现为超链接,否则呈现为纯文本。

例如,我的 xml 看起来像:

<a href='http://google.com' target='_blank'>This is the heading </a>"

如果有超链接,它应该显示一个超链接,否则将标题显示为纯文本。 我该怎么做?

在 else 标签下我无法使用下面的代码,请看下面

  <xsl:choose>
    <xsl:when test="($RTL='true') or ($RTL='True')">
      <fo:block wrap-option="wrap" hyphenate="true"  text-align="right" font-weight="bold">
        <xsl:value-of select="@friendlyname" />
      </fo:block>
    </xsl:when>
    <xsl:otherwise>
      <!--<fo:block wrap-option="wrap" hyphenate="true"  font-weight="bold">-->
      <xsl:template match="a">
        <fo:block>
          <xsl:choose>
            <xsl:when test="@href">
              <fo:basic-link>
                <xsl:attribute name="external-destination">
                  <xsl:value-of select="@href"/>
                </xsl:attribute>
                <xsl:value-of select="@friendlyname"  />
              </fo:basic-link>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@friendlyname"  />
            </xsl:otherwise>
          </xsl:choose>
        </fo:block>

      </xsl:template>

      <!--<xsl:value-of select="@friendlyname"  />-->

      <!--</fo:block>-->
    </xsl:otherwise>
  </xsl:choose>
</xsl:if>

我如何在那里使用它?

【问题讨论】:

  • 这是使用xsl:template 元素的错误方式。它不能在otherwise 内。所以,现在(即,因为您更新了问题),您的问题与基本链接无关,而是与编写 XSLT 代码的基本规则有关。

标签: c# .net xslt xsl-fo


【解决方案1】:

要在 XSL-FO 中显示链接,请使用 fo:basic-link。详情请见the relevant part of the specification

这将创建一个简单的、可点击的链接,没有任何其他格式。也就是说,格式是从周围的块元素继承的。因此,如果您的链接应加下划线或显示为蓝色,则必须明确指定。例如,通过使用 fo:inline 元素。

现在,就 XSLT 代码而言,如果您遇到 a 元素:

<xsl:template match="a">
 <fo:block><!--This is the heading block-->

测试是否有href属性:

  <xsl:choose>
    <xsl:when test="@href">
      <fo:basic-link>
        <xsl:attribute name="external-destination">
          <xsl:value-of select="@href"/>
        </xsl:attribute>
        <xsl:value-of select="."/>
      </fo:basic-link>
    </xsl:when>

另一方面,如果没有这样的属性:

    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
 </fo:block>

</xsl:template>

基本链接可以有外部或内部目的地。例如,后者用于引用目录中的特定章节。

【讨论】:

  • 每次它在其他情况下进入并且从不拾取超链接?这是我的实际值这是标题
  • 我评论了你原来的帖子。看来你把我的答案插错了地方。 xsl:choose 仅在周围模板与 a 元素匹配时才能按预期工作。
  • 感谢 Mathias 的帮助。它似乎运行良好,除了我没有从下面的代码 我的输入在顶部
  • @friendlyname is 这是标题,我只需要那个超链接,你能帮我做吗?
  • 我向您展示的代码正是做到了这一点。但我想说,与其问问题,不如花几分钟看一下 XSLT 简介——这将帮助你理解我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
相关资源
最近更新 更多