Saturday, 6 August 2016

xslt function substring-between()


Need to return a substring between two strings


xslt function


<xsl:stylesheet xmlns:xsl="" 
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)"/>


Monday, 4 January 2016

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


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


use the following xpath expression


  $items := tokenize('','/')
  for $i in 1 to count($items) 
  return (
   $i, (: position  :)
   $items[$i]  (: item content :)

Friday, 20 November 2015

Xquery compare two nodesets


Compare two nodesets for equality using xquery


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


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


Example command line

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

Thursday, 6 August 2015

Return all variables and functions from an xquery module using xslt


Require an XML listing of all variables and functions used in an xquery module


The following XSLT is a quick and dirty method to extract all the variables and functions from an xquery module into an XML representation which can then be transformed to the required format

The transformation is XSLT2 and can be invoked by use of saxon using the following command line, where thisXSLT.xsl is the code below:

java -jar saxon.jar -it:main -xsl:thisXSLT.xsl -o:result.xml "csvFile=myfile.csv"



 <xsl:param name="xquery" as="xs:string" >utils.xq</xsl:param>
 <xsl:output indent="yes"/>

 <xsl:variable name="input-text" 
              select="unparsed-text($xquery, 'utf-8')"/>

 <xsl:template match="/" name="main">
   <xsl:analyze-string select="$input-text" 
      <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
         <name datatype="{if (matches(regex-group(6), 'as ')) then substring-after(regex-group(6), 'as ') else 'undefined'}"><xsl:value-of select="normalize-space(regex-group(3))"/></name>
         <!--<xsl:value-of select="."/>-->
      Non-matching line "<xsl:value-of select="."/>"
   <xsl:variable name="regexFnStart">\{</xsl:variable>
   <xsl:analyze-string select="$input-text" 
     <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
        <name datatype="{if (matches(regex-group(7), 'as ')) then substring-after(regex-group(7), 'as ') else 'undefined'}"><xsl:value-of select="normalize-space(regex-group(3))"/></name>
        <xsl:for-each select="$arguments">
         <xsl:variable name="datatype" as="xs:string?" select="if (matches(., ' as ')) then substring-after(., ' as ') else ''"/>
         <argument datatype="{if (matches($datatype,'\)$') and not(matches($datatype,'\(\)$'))) then replace($datatype, '\)$' ,'') else $datatype}">
          <xsl:value-of select="normalize-space(if (matches(., ' as ')) then substring-before(., ' as ') else .)"/>
       <!--<xsl:value-of select="."/>--></function>
     Non-matching line "<xsl:value-of select="."/>"

Friday, 17 April 2015

xml validator

A quick an easy command line XML validator can be found at:

This is a simple java wrapper for xerces - what more could one want for simple validation

Even better why not set the path in the path environment variable and then call it from anywhere. sorted