Jump to content

Generating Links From Keywords


DanielShillock

Recommended Posts

Hello everyone, I have a problem regarding XML/XSLT. I would like to have a repository of keywords for a document that I am writing. These keywords will appear in text written in the associated XML documents and from here I would like XSLT to transform those words into hyperlinks. I know this is possible using something like Saxon however I would like to do it clientside if possible. Here is an example of what I would like to achieve. document.xml

<document>  <p>This is an example of text. I would like the word car become a to hyperlink</p></document>

keywords.xml

<keywordlist><keywords>car</keywords><keywords>plane</keywords><keywords>boat</keywords></keywordlist>

car.xml

<document>  <p>Cars are great!</p></document>

I would like the output to be (HTML) as the following:

<p>This is an example of text. I would like the word <a href="car.xml">car</a> to become a hyperlink.

Link to comment
Share on other sites

I have solved this problem. For anyone interested in a solution here it is. I am using this xslt markup from the following link: http://www.jenitennison.com/xslt/utilities/markup.xml I have a document called 'keywords.xml' in the same directory as my content. Here is how I search through the text. import markup.xsl

<xsl:import href="markup.xsl" />

Look for the tags in which you want to search through. I have used the <p> tag and the <li> tag but they could be anything you have content in.

<xsl:template match="p">  <p>    <xsl:call-template name="markup">	  <xsl:with-param name="text" select="." />	  <xsl:with-param name="phrases" select="document('keywords.xml')/document/keywords/keyword" />	  <xsl:with-param name="first-only" select="false()" />    </xsl:call-template>  </p></xsl:template>

Finally wrap the found text as a hyperlink

<xsl:template match="keyword" mode="markup">  <xsl:param name="word" />  <a href="{$word}.xml">    <xsl:value-of select="$word" />  </a></xsl:template>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...