Skip to content
Renaud Guillard edited this page Dec 8, 2020 · 9 revisions

XSH Examples

Functions definition

XSH source

#!xml
<?xml version="1.0" encoding="UTF-8"?>
<xsh:functions xmlns:xsh="http://xsd.nore.fr/xsh">
	<xsh:function name="ns_realpath">
		<xsh:parameter name="path" />
		<xsh:body>
		<xsh:local name="cwd">$(pwd)</xsh:local>
		<![CDATA[
[ -d "${path}" ] && cd "${path}" && path="."
while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done

if [ -d "${path}" ]
then
	path="$( cd -P "$( dirname "${path}" )" && pwd )"
else
	path="$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
fi

cd "${cwd}" 1>/dev/null 2>&1
echo "${path}"
		]]></xsh:body>
	</xsh:function>
</xsh:functions>

Bash output

#!bash
xsltproc ${NS_XML}/ns/xsl/languages/xsh.xsl examples.xsh

#!bash
ns_realpath()
{
	local path
	if [ $# -gt 0 ]
	then
		path="${1}"
		shift
	fi
	local cwd="$(pwd)"
	[ -d "${path}" ] && cd "${path}" && path="."
	while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done
	
	if [ -d "${path}" ]
	then
		path="$( cd -P "$( dirname "${path}" )" && pwd )"
	else
		path="$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
	fi
	
	cd "${cwd}" 1>/dev/null 2>&1
	echo "${path}"
}

Ksh output

#!bash
xsltproc --stringparam xsh.defaultInterpreterType ksh ${NS_XML}/ns/xsl/languages/xsh.xsl examples.xsh

#!bash
function ns_realpath
{
	typeset var path
	if [ $# -gt 0 ]
	then
		path="${1}"
		shift
	fi
	typeset var cwd="$(pwd)"
	[ -d "${path}" ] && cd "${path}" && path="."
	while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done
	
	if [ -d "${path}" ]
	then
		path="$( cd -P "$( dirname "${path}" )" && pwd )"
	else
		path="$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
	fi
	
	cd "${cwd}" 1>/dev/null 2>&1
	echo "${path}"
}

A simple program

#!xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Interpreter type can be forced through the @interpreterType attribute -->
<xsh:program interpreterType="bash" xmlns:xsh="http://xsd.nore.fr/xsh">
	<xsh:functions>
		<!-- This will include ns_realpath function definition --> 
		<xi:include href="relativepath/to/filesystem.xsh" 
			xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function[@name = 'ns_realpath'])" />
	</xsh:functions>
	<!-- The program body -->
	<xsh:code><![CDATA[
scriptFilePath="$(ns_realpath "${0}")"
echo "Hello from ${scriptFilePath}" 
	]]></xsh:code>
</xsh:program>

Adding program interface definition

Here is the content of the XSH file for the <<file apps/build-shellscript>> tool

#!xml
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!-- Copyright &copy; 2011-2012 by Renaud Guillard (dev@nore.fr) --&gt;
&lt;!-- Distributed under the terms of the MIT License, see LICENSE --&gt;
&lt;xsh:program interpreterType="bash" 
		xmlns:prg="http://xsd.nore.fr/program" 
		xmlns:xsh="http://xsd.nore.fr/xsh" 
		xmlns:xi="http://www.w3.org/2001/XInclude"&gt;
	&lt;xsh:info&gt;
		&lt;!-- The program interface XML definition file --&gt;
		&lt;xi:include href="build-shellscript.xml" /&gt;
	&lt;/xsh:info&gt;
	&lt;xsh:functions&gt;
		&lt;xi:include href="../lib/filesystem/filesystem.xsh" xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function[@name = 'ns_realpath'])" /&gt;
		&lt;xi:include href="functions.xsh" xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function)" /&gt;
	&lt;/xsh:functions&gt;
	&lt;xsh:code&gt;
		&lt;!-- We can use XInclude here too --&gt;
		&lt;xi:include href="build-shellscript.body.init.sh" parse="text"/&gt;
		&lt;xi:include href="build-shellscript.body.process.sh" parse="text"/&gt;
	&lt;/xsh:code&gt;
&lt;/xsh:program&gt;

XML shell file overview

Clone this wiki locally