Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperlinks within XSLT Templates

Tags:

xslt

I am trying to create hyperlinks using XML information and XSLT templates. Here is the XML source.

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

I want the output to look like this, with the company names being hyperlinks based on the "smartTextLink" tags in the Xml.

Among individual stocks, the top percentage gainers in the S.&P. 500 are Eastman Kodak Co and Huntington Bancshares Inc.

Here are the templates that I am using right now. I can get the text to display, but not the hyperlinks.

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

I have tried multiple variations to try to get the hyperlinks to work correctly. I am thinking that the template match="smartTextLink" is not being instantiated for some reason. Does anyone have any ideas on how I can make this work?

EDIT: After reviewing some of the answers, it is still not working in my overall application.

I am calling the smartText template from within my main template

using the following statement...

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

Could this also be a part of the problem?

Thank you

Shane

like image 314
Grizzly Peak Software Avatar asked Dec 22 '22 12:12

Grizzly Peak Software


1 Answers

Either move the xsl:attribute before any children, or use an attribute value template.

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

From the creating attributes section of the XSLT 1 spec:

The following are all errors:

  • Adding an attribute to an element after children have been added to it; implementations may either signal the error or ignore the attribute.
like image 194
Pete Kirkham Avatar answered Jan 05 '23 22:01

Pete Kirkham