Saturday, 28 October 2017

XSLT functions for distance conversions

Here are two simple XSLT functions for converting between miles and km

 <xsl:function name="fn:miles-to-km">
  <xsl:param name="miles" as="xs:decimal"/> 
  <xsl:sequence select="format-number($miles * 1.609347, '0.0')"/>
 </xsl:function>
 
 <xsl:function name="fn:km-to-miles">
  <xsl:param name="km" as="xs:decimal"/> 
  <xsl:sequence select="format-number($km * 0.621371, '0.0')"/>
 </xsl:function>

Wednesday, 4 October 2017

XSLT to convert Excel (xls) to html

Issue

Require html from an xls (Excel) spreadsheet

Resolution

Quick and dirty XSLT transformation

Xquery

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="ss"
         version="2.0">
 <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
       <head>
        <style>
         <xsl:apply-templates select="ss:Workbook/ss:Styles/ss:Style"/>
        </style>
       </head>
     <body>
      <div id="layout2">
       <xsl:apply-templates select="ss:Workbook/ss:Worksheet/ss:Table"/>
      </div>
     </body>
    </html>
  </xsl:template>
 
  <xsl:template match="ss:Style">
   <xsl:text>#</xsl:text>
   <xsl:value-of select="@ss:ID"/>
   <xsl:text> {</xsl:text>
   <xsl:if test="ss:Alignment/@ss:Horizontal">
   <xsl:text>text-align: </xsl:text>
   <xsl:value-of select="ss:Alignment/@ss:Horizontal"/>
   <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Alignment/@ss:Vertical">
   <xsl:text>vertical-align: </xsl:text>
   <xsl:value-of select="ss:Alignment/@ss:Vertical"/>
   <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Alignment/@ss:Vertical">
   <xsl:text>vertical-align: </xsl:text>
   <xsl:value-of select="ss:Alignment/@ss:Vertical"/>
   <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Font/@ss:Bold=1">
   <xsl:text>font-weight: bold;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Font/@ss:Color">
   <xsl:text>color: </xsl:text>
   <xsl:value-of select="ss:Font/@ss:Color"/>
   <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Font/@ss:StrikeThrough=1">
   <xsl:text>text-decoration: line-through;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Font/@ss:Underline='Single'">
   <xsl:text>text-decoration: underline;</xsl:text>
  </xsl:if>
  <xsl:if test="ss:Interior/@ss:Color">
   <xsl:text>background-color: </xsl:text>
   <xsl:value-of select="ss:Interior/@ss:Color"/>
   <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:for-each select="ss:Borders/ss:Border">
   <xsl:if test="@ss:Color">
    <xsl:text>border-</xsl:text>
    <xsl:value-of select="@ss:Position"/>
    <xsl:text>-color:</xsl:text>
    <xsl:value-of select="@ss:Color"/>
    <xsl:text>;</xsl:text>
   </xsl:if>
   <xsl:if test="@ss:Weight">
      <xsl:text> border-</xsl:text>
      <xsl:value-of select="@ss:Position"/>
      <xsl:text>-width:</xsl:text>
      <xsl:value-of select="@ss:Weight"/>
      <xsl:text>px;</xsl:text>
   </xsl:if>
   <xsl:if test="@ss:LineStyle">
    <xsl:text>border-</xsl:text><xsl:value-of select="@ss:Position"/><xsl:text>-style:</xsl:text>
    <xsl:value-of select="@ss:LineStyle"/>
    <xsl:text>;</xsl:text>
   </xsl:if>
  </xsl:for-each>
  <xsl:text>}</xsl:text>
  <xsl:text>.scrolltable {overflow: scroll;}</xsl:text>
 </xsl:template> 
 
 <xsl:template match="ss:Table">
  <div class="scrolltable">
   <table>
    <xsl:apply-templates select="ss:Row"/>
   </table>
  </div>
 </xsl:template>
 
 <xsl:template match="ss:Row">
  <tr>
   <xsl:apply-templates select="ss:Cell"/>
  </tr>
 </xsl:template>
 
 <xsl:template match="ss:Row[@ss:Hidden = '1']" priority="10"/> 
 
 <xsl:template match="ss:Cell">
  <td>
   <xsl:attribute name="id">
    <xsl:value-of select="@ss:StyleID"/>
   </xsl:attribute>
   <xsl:if test="@ss:MergeAcross">
    <xsl:attribute name="colspan">
     <xsl:value-of select="@ss:MergeAcross+1"/>
    </xsl:attribute>
   </xsl:if>
   <xsl:value-of select="ss:Data"/>
  </td>
 </xsl:template>
 
</xsl:stylesheet>

Saturday, 6 August 2016

xslt function substring-between()

Issue

Need to return a substring between two strings

Resolution

xslt function

code

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:gm='http://www.griffmonster.org/xslt2/functions' 
version="2.0" exclude-result-prefixes="gm">

<xsl:function name="gm:substring-between">
 <xsl:param name="string"/>
 <xsl:param name="start-string"/>
 <xsl:param name="end-string"/>
 <xsl:sequence select="substring-before(substring-after($string, $start-string),$end-string)"/>
</xsl:function>



</xsl:stylesheet>

Monday, 4 January 2016

Determine the position and content of an item in a string sequence

Issue

We have a sequence of strings and for each item we need the position of the item within the sequence

Resolution

use the following xpath expression

Xquery

let
  $items := tokenize('xslt-tricks.blogspot.co.uk/search/label/xquery','/')
return
  for $i in 1 to count($items) 
  return (
   $i, (: position  :)
   $items[$i]  (: item content :)
  )

Friday, 20 November 2015

Xquery compare two nodesets

Issue

Compare two nodesets for equality using xquery

Resolution

Use a recursive function to runa deep-equal() function across all nodes

Xquery

declare function utils:deep-equal($node1 as element(), $node2 as element()) as xs:boolean {
    let $equal as xs:boolean := deep-equal($node1, $node2)
    return (
      if (not($equal) or empty($node1/*)) then $equal
      else  (
        every $de in (
            for $n in $node1/*
            let $position := count($n/preceding-sibling::*)+1
            return utils:deep-equal($n, $node2/*[$position])
            )
        satisfies $de = true()
        )
    )
};

Thursday, 22 October 2015

MarkLogic fails to install with 'Rolling Back Action' Message

In a recent MarkLogic upgrade on Windows Server 2008 the following issue was encountered:

The uninstall of the previous version successfully completed. The upgrade installer was initiated and it went thorugh the install process. At the very end it posted a message 'Rolling Back Action' and promprly reversed the installation.

The issue was caused by the inital uninstall not removing the MarkLogic service. Check this in the task manager services panel. If the MArkLogic service is there then run the command line with the following command:

sc delete MarkLogic

If this fails to remove the service but returns a message along the lines of 'this has been marked for deletion', then reboot the machine and the service should get removed. The installation can then be run successfully

Thursday, 27 August 2015

Monitor log files in real time

use the tail unix command for which there are windows equivalents

Links

http://tailforwin32.sourceforge.net/index.php

http://unxutils.sourceforge.net

Example command line

@D:\Logs\tail -f D:\Squid\var\logs\access.log