xslutil Ver 1.0
A XSLT library which is a set of utilities for XSLT programs on Web browsers.
Copyright (C) xslet project.
MIT License.
About xslutil
xslutil is a XSL library for string operations. XSLT1 and XPath1 provides some functions for string operations, but those are not enough. This library provides some other functions which are needed in xslet project.
Usage
This section explains how to import xslutil and call its templates in your XSL application or library.
Import xslutil
To import xslutil.xsl into another XSL file, the following handworks are needed.
-
Add the namespace declaration of xslutil in a XSL file. The namespace of xslutil is "https://github.com/xslet/2020/xslutil".
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:ut="https://github.com/xslet/2020/xslutil"> <!-- This! -->... -
Add a import element of xslutil in the XSL file.
...<xsl:import href="./xslutil.xsl"/>...Call templates
xslutil provides the following named-templates:
- ut:count
- ut:ends_with
- ut:get_dir_from_url
- ut:get_namespace_url
- ut:get_xsl_url
- ut:is_absolute_url
- ut:repeat
- ut:replace
- ut:trim
- ut:trim_start
- ut:trim_end
- ut:validate
ut:count
Counts a target substring in a string. This function returns a positive number or zero.
<!-- Counts 'AB' in 'ABAAABCA' then return 2 --><xsl:call-template name="ut:count"><xsl:with-param name="string">ABAABCA</xsl:with-param><xsl:with-param name="target">AB</xsl:with-param></xsl:call-template>ut:ends_with
Tests that a string ends with a suffix substring. This function returns
$ut:true
(='true'
) or empty.<!-- Tests that 'ABC' ends with 'BC' then return 'true' --><xsl:call-template name="ut:ends_with"><xsl:with-param name="string">ABC</xsl:with-param><xsl:with-param name="suffix">BC</xsl:with-param></xsl:call-template>ut:get_dir_from_url
Returns a parent url of url. This function returns a url string or
'.'
.<!-- Returns 'https://domain/dir/' from 'https://domain/dir/file' --><xsl:call-template name="ut:get_dir_from_url"><xsl:with-param name="url">https://domain/dir/file</xsl:with-param></xsl:call-template>ut:get_namespace_url
Returns the namespace URI of prefix. This function returns a URI string, or
$ut:unknown_namespace
.<!-- Returns 'https://github.com/xslet/2020/xslutil' which is the namespace URI of the prefix 'ut'. --><xsl:call-template name="ut:get_namespace_uri"><xsl:with-param name="prefix">ut</xsl:with-param></xsl:call-template>NOTE: Firefox does not support the XPath's namespace axis:
`namespace::*`
. So, on Firefox, this function finds an element using the target namespace then applys`namespace-uri()`
to it. If such an element is not found, this function returns`$ut:unknown_namespace`
.ut:get_xsl_url
Returns the XSL URL from the processing instruction or the specified pi.
<?xml version="1.0" encoding="utf-8"?><?xml-styesheet type="application/xml" href="https://domain/path/to/file.xsl"?>...<!-- Returns 'https://domain/path/to/file.xsl' --><xsl:call-template name="ut:get_xsl_url"/><!-- Returns 'path/to/file.xsl' --><xsl:call-template name="ut:get_xsl_url"><xsl:with-param name="pi" select='<?xml-stylesheet type="text/xsl" href="path/to/file.xsl"?>'/></xsl:call-template>ut:is_absolute_url
Tests that a url string is an absolute URL. This function returns
$ut:true
(='true'
) or empty.<!-- Tests if 'https://aaa/bbb/ccc' is absolute, then returns 'true' --><xsl:call-template name="ut:is_absolute_url"><xsl:with-param name="url">https://aaa/bbb/ccc</xsl:with-param></xsl:call-template><!-- Tests if 'aaa/bbb/ccc' is absolute, then returns empty --><xsl:call-template name="ut:is_absolute_url"><xsl:with-param name="url">aaa/bbb/ccc</xsl:with-param></xsl:call-template>ut:repeat
Repeats string count times.
<!-- Returns 'ABCABCABC' --><xsl:call-template name="ut:repeat"><xsl:with-param name="string">ABC</xsl:with-param><xsl:with-param name="count">3</xsl:with-param></xsl:call-template>ut:replace
Replaces all target substrings to replacement in string.
<!-- Returns 'AdeAde' --><xsl:call-template name="ut:replace"><xsl:with-param name="string">ABCABC</xsl:with-param><xsl:with-param name="target">BC</xsl:with-param><xsl:with-param name="target">de</xsl:with-param></xsl:call-template>ut:trim
Trims target substrings in both side of string.
<!-- Returns 'ABC' --><xsl:call-template name="ut:trim"><xsl:with-param name="string"> ABC </xsl:with-param></xsl:call-template><!-- Returns ' ABC ' --><xsl:call-template name="ut:trim"><xsl:with-param name="string">## ABC ###</xsl:with-param><xsl:with-param name="target">#</xsl:with-param></xsl:call-template>ut:trim_start
Trims target substrings in start of string.
<!-- Returns 'ABC ' --><xsl:call-template name="ut:trim_start"><xsl:with-param name="string"> ABC </xsl:with-param></xsl:call-template><!-- Returns ' ABC ###' --><xsl:call-template name="ut:trim_start"><xsl:with-param name="string">## ABC ###</xsl:with-param><xsl:with-param name="target">#</xsl:with-param></xsl:call-template>ut:trim_end
Trims target substrings in end of string.
<!-- Returns ' ABC' --><xsl:call-template name="ut:trim_end"><xsl:with-param name="string"> ABC </xsl:with-param></xsl:call-template><!-- Returns '## ABC ' --><xsl:call-template name="ut:trim_end"><xsl:with-param name="string">## ABC ###</xsl:with-param><xsl:with-param name="target">#</xsl:with-param></xsl:call-template>ut:validate
Tests string contains one of character in forbidden. If forbidden substrings are contained, this function returns a default string. Otherwise this function returns the tested string.
<!-- Returns 'def' --><xsl:call-template name="ut:validate"><xsl:with-param name="string">abc</xsl:with-param><xsl:with-param name="forbidden">c</xsl:with-param><xsl:with-param name="default">def</xsl:with-param></xsl:call-template><!-- Returns 'abc' --><xsl:call-template name="ut:validate"><xsl:with-param name="string">abc</xsl:with-param><xsl:with-param name="forbidden">z</xsl:with-param><xsl:with-param name="default">def</xsl:with-param></xsl:call-template>APIs
The API document of xslutil is generated with xsldoc.
The generated API document is here.Tests
This section provides the results of unit tests of named-templates written in the API document.
Links
xslutil is one of libraries in xslet project. xslet is a project to develop Web applications/libraries with XSL (XSLT and XPath).
XSLT and XPath is the specifications specified by W3C. xslutil uses XSLT version 1.0 and XPath version 1.0 in it because Web browsers support them.
In addition, xslutil uses XSLT version 3.0 and XPath version 3.0 in the build environment.
The xslutil also uses following application as tools in the build environment.