create hyper links dynamically when transforming x

2019-03-06 16:47发布

问题:

I want to create a heading in my PDF report using my xsl file. If the source file contains a hyperlink then it should render it as hyperlink, otherwise as plain text.

For example, my xml looks like:

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

It should display a hyperlink if there is one, otherwise display the heading as plain text. How can I do that?

I am not able to use the below code under otherwise tag , please see below

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

How can I use it there?

回答1:

To display a link in XSL-FO, use fo:basic-link. See the relevant part of the specification for details.

This creates a simple, clickable link without any formatting otherwise. That is, the format is inherited from the surrounding block element. So, if your links should be underlined or shown in blue, you have to specify this explicitly. For instance, by using a fo:inline element.

Now, in terms of XSLT code, if you encounter a elements:

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

Test whether there is a href attribute or not:

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

On the other hand, if there is no such attribute:

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

</xsl:template>

Basic links can have either an external or internal destination. The latter is used to refer to specific chapters from the table of contents, for example.