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

Thursday, 6 August 2015

Return all variables and functions from an xquery module using xslt

Issue

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

Resolution

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"

XSLT

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">

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

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

 <xsl:template match="/" name="main">
  <xquery>
  <variables>
   <xsl:analyze-string select="$input-text" 
        regex="(declare)\s+(variable)\s+\$((\c)+:(\c)+)(\s+as\s+.+)?\s+(:=)"> 
     <xsl:matching-substring>
      <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
        <variable>
         <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="."/>-->
        </variable>
     </xsl:matching-substring>
     <xsl:non-matching-substring>
      <xsl:message>
      Non-matching line "<xsl:value-of select="."/>"
      </xsl:message>
     </xsl:non-matching-substring>
    </xsl:analyze-string>
  </variables>
  <functions>
   <xsl:variable name="regexFnStart">\{</xsl:variable>
   <xsl:analyze-string select="$input-text" 
       regex="(declare)\s*(function)\s+((\c)+)\(((.)*)\)(\s+as\s+.+)?\s+({$regexFnStart})"> 
    <xsl:matching-substring>
     <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
       <function>
        <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 .)"/>
         </argument>
        </xsl:for-each>
       <!--<xsl:value-of select="."/>--></function>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
     <xsl:message>
     Non-matching line "<xsl:value-of select="."/>"
     </xsl:message>
    </xsl:non-matching-substring>
   </xsl:analyze-string>
  </functions>
  </xquery>
 </xsl:template>

Friday, 17 April 2015

xml validator

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

https://github.com/amouat/xsd-validator

http://www.adrianmouat.com/bit-bucket/2013/11/xml-schema-validation/

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