2014-01-24 51 views
-1

我想使用我的xsl文件在我的PDF报告中创建一个标题。如果源文件包含超链接,则应将其呈现为超链接,否则为纯文本。使用xlst将xml转换为xsl-fo时动态创建超级链接?

例如,我的XML看起来像:

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

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

我不能够使用,否则标签下,下面的代码,请参见下面

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

我如何使用它呢?

+0

这是使用'xsl:template'元素的不正确方法。它不能在'否则'里面。所以,现在(即自更新了您的问题以来),您的问题与基本链接无关,但具有编写XSLT代码的基本规则。 –

回答

0

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

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

+0

你现在可以看看更新后的描述吗? –

+0

每当它进入其他条件并从不拾起超链接?这是我的实际价值This is the heading

+0

我对您的原帖发表了评论。看来,你把我的答案插入了错误的地方。如果周围的模板匹配'a'元素,'xsl:choose'只能按照预期工作。 –