Thursday, 25 July 2013

Solr XSLT alternative with live links

The XSLT files that Solr has standard are pretty good and cover a wide variety of different transforms however they didnt give me exactly what I wanted.  I use Nutch as a filesystem crawler and therefore wanted the XSLT transform to make all the ur'ls(uri's) to the crawled files to be hyperlinks, so that when I clicked on them it would either download that file or allow me to view it in my browser.  I therefore decided that I would make my own XSLT file even though I had no previous experience using XSLT before so I have adapted the example.xsl file that comes standard with Solr and this is the final version:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0'
  <xsl:output media-type="text/html" encoding="UTF-8"/> 
  <xsl:variable name="title" select="concat('Solr search results (',response/result/@numFound,' documents)')"/>
  <xsl:template match='/'>
        <title><xsl:value-of select="$title"/></title>
        <xsl:call-template name="css"/>
        <h1><xsl:value-of select="$title"/></h1>
        <div class="note">
          ******* Author: Allan Macmillan ******** ********* allan2.xls
        <xsl:apply-templates select="response/result/doc"/>
  <xsl:template match="doc">
    <xsl:variable name="pos" select="position()"/>
    <div class="doc">
      <table width="100%">
          <xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
  <xsl:template match="doc/*[@name='score']" priority="100">
    <xsl:param name="pos"></xsl:param>
      <td class="name">
        <xsl:value-of select="@name"/>
      <td class="value">
        <xsl:value-of select="."/>
        <xsl:if test="boolean(//lst[@name='explain'])">
          <xsl:element name="a">
            <!-- can't allow whitespace here -->
            <xsl:attribute name="href">javascript:toggle("<xsl:value-of select="concat('exp-',$pos)" />");</xsl:attribute>?</xsl:element>
          <xsl:element name="div">
            <xsl:attribute name="class">exp</xsl:attribute>
            <xsl:attribute name="id">
              <xsl:value-of select="concat('exp-',$pos)" />
            <xsl:value-of select="//lst[@name='explain']/str[position()=$pos]"/>
  <xsl:template match="doc/arr" priority="100">
      <td class="name">
        <xsl:value-of select="@name"/>
      <td class="value">
        <xsl:for-each select="*">
          <li><xsl:value-of select="."/></li>
  <xsl:template match="doc/*">
      <td class="name">
        <xsl:value-of select="@name"/>
      <td class="value">
      <xsl:variable name="var" select="."/>
         <xsl:when test="starts-with($var,'file:////')">
            <xsl:attribute name="href">
               <xsl:value-of select="$var"/>
     <xsl:value-of select="$var"/></a>
             <xsl:value-of select="$var"/>
  <xsl:template match="*"/>
  <xsl:template name="css">
      function toggle(id) {
        var obj = document.getElementById(id); = ( != 'block') ? 'block' : 'none';
    <style type="text/css">
      body { font-family: "Lucida Grande", sans-serif } { font-style: italic; font-size:80%; }
      td { vertical-align: top; }
      ul { margin: 0px; margin-left: 1em; padding: 0px; }
      .note { font-size:80%; }
      .doc { margin-top: 1em; border-top: solid grey 1px; }
      .exp { display: none; font-family: monospace; white-space: pre; }

No comments:

Post a Comment