4

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

4
  • Use "<xsl:apply-tempates select="marketSummaryModuleData/smartText" />", instead of "<xsl:value-of ... />". Commented Apr 1, 2009 at 16:36
  • Since this is in the middle of a <div> I am getting a parsing error. Commented Apr 1, 2009 at 16:40
  • This is not possible, you can use "apply-tempates" any place where "value-of" would work. Can you post more context? I guess the error is somewhere else. Commented Apr 1, 2009 at 16:43
  • I was able to clear the error. It was a typo. It is now working, using the first solution. Commented Apr 1, 2009 at 16:50

2 Answers 2

6

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.
Sign up to request clarification or add additional context in comments.

4 Comments

A naked <xsl:apply-templates /> would suffice.
True, but that wasn't the question.
It wasn't meant to be critical. It was just a comment. :-)
This worked. The problem was partly due to the way that I was applying the template within my main template. Thanks you to all!
5

Try this - worked for me:

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

Trick is - <xsl:attribute> first, before you do any other processing.

Marc

2 Comments

The <xsl:attribute> was not visible therefore your answer seemed a bit strange. Just corrected it. (and +1)
Thank you for your answers. This was helpful, Even though I did not vote it the correct answer (the other answer required fewer lines of code.). I did vote this one up. Thanks to all!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.