RE: [xsl] adding links to html using xsl

Subject: RE: [xsl] adding links to html using xsl
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Wed, 19 May 2004 11:35:43 +0100
> i.e I want to add links to the existing text.
> How can this be done by xslt. 
> Is there any method to scan through the html file and
> search for the the string after 'Analysis of Package:'
> and add a link and leave the rest of the code as it is
> in a newly generated html file.

Yes its perfectly possible, but reasonably difficult.  The problem lies
in whitespace and how much of it is between the text node you want to
wrap in anchor tags, and the preceding text node that you are checking
against.  You could walk the preceding axis for each text node and check
that the first occurance of any non-whitespace text was the text you are
looking for - or you can do it this way, which 'lifts' the text out of
the markup into a variable which can then be queried as you process each
text node in the stylesheet.  This kind of problem is one of the reasons
why xml was invented - extracting information out of html is very
difficult.

This stylesheet:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:exsl="http://exslt.org/common";
  extension-element-prefixes="exsl">

<xsl:param name="searchString" select="'Analysis of Package:'"/>

<xsl:variable name="extractedWords-rtf">
  <root>
    <xsl:for-each select="//text()">
      <xsl:if test="normalize-space()">
        <sentence id="{generate-id()}"><xsl:value-of
select="normalize-space(.)"/></sentence>
      </xsl:if>
    </xsl:for-each>
  </root>
</xsl:variable>
<xsl:variable name="extractedWords"
select="exsl:node-set($extractedWords-rtf)"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()">
  <xsl:variable name="genId" select="generate-id()"/>
  <xsl:choose>
    <xsl:when test="$genId = $extractedWords/root/sentence/@id">
      <xsl:choose>
        <xsl:when test="$extractedWords/root/sentence[@id =
$genId]/preceding-sibling::sentence[1] = $searchString">
          <a href="xyz.xml"><xsl:value-of select="."/></a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
</xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Applied to this xml (closing tags added):

<html>
	<body>
		<h1>
			<center>Findbugs Summary Report</center>
		</h1>
.....
.....
		<table border="0" width="90%">
			<tr>
.....
.....
				<h2>Analysis of Package: <i>
						<font
color="green">dcx.sysman.logging.format</font>
					</i>
				</h2>
.....
.....
			</tr>
		</table>
	</body>
</html>


Produces this result:

<html>
   <body>	
      <h1>
         <center>Findbugs Summary Report</center>         		
      </h1>
      .....
      .....      		
      <table border="0" width="90%">
         			
         <tr>
            .....
            .....
            				
            <h2>Analysis of Package: <i>
                  						<font
color="green"><a href="xyz.xml">dcx.sysman.logging.format</a></font>
           					</i>

            </h2>
            .....
            .....
            			
         </tr>         		
      </table>      	
   </body>   
</html>

Current Thread