XSLT模板中的超链接

时间:2009-04-01 16:19:22

标签: xslt

我正在尝试使用XML信息和XSLT模板创建超链接。这是XML源代码。

<smartText>
Among individual stocks, the top percentage gainers in the S. and P. 500 are 
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=HBAN.O">Huntington Bancshares Inc</smartTextLink>
and 
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=EK">Eastman Kodak Co</smartTextLink>
.
</smartText>

我希望输出看起来像这样,公司名称是基于Xml中“smartTextLink”标签的超链接。

在个股中,S&amp; P的涨幅最大。 500名是Eastman Kodak Co和Huntington Bancshares Inc.

以下是我正在使用的模板。我可以显示文本,但不能显示超链接。

<xsl:template match="smartText">
  <p class="smartText">
    <xsl:apply-templates select="child::node()" />
  </p>
</xsl:template>

<xsl:template match="smartTextLink">
  <a>
    <xsl:apply-templates select="child::node()" />
    <xsl:attribute name="href">
      <xsl:value-of select="@smartTextRic"/>
    </xsl:attribute>
  </a> 
</xsl:template>      

我尝试过多种变体,试图让超链接正常工作。我认为模板匹配=“smartTextLink”由于某种原因没有被实例化。有没有人对如何使这项工作有任何想法?

编辑:在查看了一些答案后,它仍然无法在我的整个应用程序中运行。

我在主模板中调用了smartText模板

使用以下声明......

<xsl:value-of select="marketSummaryModuleData/smartText"/>   

这也可能是问题的一部分吗?

谢谢

沙恩

2 个答案:

答案 0 :(得分:6)

在任何孩子之前移动xsl:attribute,或使用attribute value template

<xsl:template match="smartTextLink">
    <a href="{@smartTextRic}">
        <xsl:apply-templates/>
    </a> 
</xsl:template>

来自XSLT 1规范的creating attributes部分:

  

以下是所有错误:

     
      
  • 在添加子元素后向元素添加属性;实现可以发出错误信号或忽略属性。
  •   

答案 1 :(得分:5)

试试这个 - 为我工作:

<xsl:template match="smartText">
    <p class="smartText">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="smartTextLink">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="@smartTextRic"/>
      </xsl:attribute>
      <xsl:value-of select="text()"/>
    </a>
  </xsl:template>

在您进行任何其他处理之前,先行动 - <xsl:attribute>

马克