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

Thursday 26 March 2015

XSLT group by postion

Issue

Need to create individual files each with n nodes from the master file

Resolution

There is a simple resolution to this although it is specific to XSLT 2. This uses the for-each-group routine and the idiv operator. idiv is an integer division which is equivalent to (x div y) cast as xs:integer

Consider a nodeset held as a variable $nodes and a group size of nodes defined by the integer variable $groupsize, then using the following routine will split the nodeset into distinct files with $groupsize nodes to each file

XSLT

<xsl:for-each-group select="$nodes" 
    group-adjacent="(position() - 1) idiv $groupsize">
 
    <xsl:result-document href="{concat('resultdoc', format-number(position(),'000'), '.xml')}">
        <result>
            <xsl:sequence select="current-group()"/>
        </result>
    </xsl:result-document>
</xsl:for-each-group>

Thursday 12 March 2015

netstat switches

The following switches can be used with the ipconfig command line utility:

SwitchEffect
-a Displays all connections and listening ports
-bDisplays the executable involved in creating each connection or listening port
-eDisplays Ethernet statistics
-fDDisplays Fully Qualified Domain Names for foreign addresses. (In Windows Vista/7 only)
-nDisplays addresses and port numbers in numerical form
-o Displays the owning process ID associated with each connection
-p protocol Shows connections for the protocol specified by proto; proto may be any of: TCP, UDP, TCPv6, or UDPv6.
-rDisplays the routing table
-s Displays per-protocol statistics
-tDDisplays the current connection offload state, (Windows Vista/7)
-v When used in conjunction with -b, will display sequence of components involved in creating the connection or listening port for all executables. (Windows XP SP2, SP3)
[interval] An integer used to display results multiple times with specified number of seconds between displays. Continues until stopped by command ctrl+c. Default setting is to display once

Common usage netsta -a -n -o

Use the pid to cross reference with the process running in Task Manager

Xquery: for $x at $position in $seq expression

A useful expression available to xquery is the "for x at $position in $seq expression" (for at in expression)


for $x at $position in $seq
 return
  if ($position = 1) then
   (: do something :)
  else if ($position = 2) then
   (: do something else :)
  else
   (: default :)