xsldo Ver 1.0

A XSL library to operate XML data on Web browsers.

Copyright (C) xslet project.
MIT License.

About xsldo

xsldo is a XSL library which provides a set of named-templates for XML data operation. This library enables a XSL application to operate XML data like XSLT/XPath while hiding the XSLT/XPath specification.

This library support XPath like paths to indicate XML nodes. That is, the path format concatenates node names with slashes, prefixes '@' to an attribute name and puts '[]' around a path condition. Moreover, it supports '*' for all child elements, '..' for a parent element and '//' for all descendant elements. Though this library does not conform to the XPath specifications.

This library also supports XML namespace. In addition, this library provides 'xxx:use-namespace' element ('xxx' represents a used namespace prefix) for Firefox, because the XSLT processor of Firefox does not support namespace axis.

Usage

This section explains how to import xsldo and call its templates in your XSL application or library.

Import xsldo

To import xsldo.xsl into another XSL file, the following handworks are needed. Since xsldo depends on xslutil, xslutil.xsl is needed to import, too.

  1. Add the namespace declaration of xsldo and xslutil in a XSL file. The namespace of xsldo is "https://github.com/xslet/2020/xsldo".

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ut="https://github.com/xslet/2020/xslutil"
      xmlns:do="https://github.com/xslet/2020/xsldo"> <!-- This! -->
      ...
  2. Add a import element of xsldo in the XSL file.

      ...
    &npsp; <!-- xslutil.xsl is already imported in xsldo.xsl -->
      <xsl:import href="./xsldo.xsl"/>
      ...

Call templates

xsldo provides the following named-templates:

do:for_by_path

Loops for each nodes at specified expanded paths.

<?xml version="1.0" encoding="utf-8"?>
<!-- for.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xsldo.xsl">
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="for"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="for[boolean(@path)]">
    <xsl:call-template name="do:get_objects_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="allow">|all|</with-param>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="all">
    <xsl:call-template name="do:get_objects_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="what">text</with-param>
      <xsl:with-param name="suffix">,</with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints ABC1 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="for.xsl"?>
<top>
  <for path="//aaa"><all path="bbb"/></for> <!-- Outputs 'ABC1,ABC2,ABC3,' -->
 
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

do:for_times

Loops specified times.

<?xml version="1.0" encoding="utf-8"?>
<!-- for_times.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xsldo.xsl">
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="for"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="for[boolean(@times)]">
    <xsl:call-template name="do:get_objects_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="allow">|first|</with-param>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="first">
    <xsl:call-template name="do:get_objects_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="what">text</with-param>
      <xsl:with-param name="suffix">,</with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints ABC1 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="for_times.xsl"?>
<top>
  <for path="//aaa"><first path="bbb"/></for> <!-- Outputs 'ABC1,ABC2,ABC3,' -->
 
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

do:get_objects_by_path

Gets either values, names or generated-ids of nodes at a specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- all.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xsldo.xsl">
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="all"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="all">
    <xsl:call-template name="do:get_objects_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="what">text</with-param>
      <xsl:with-param name="suffix">,</with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints ABC1,ABC2,ABC3, on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="all.xsl"?>
<top>
  <all path="//aaa/bbb"/> <!-- Outputs 'ABC1,ABC2,ABC3,' -->
 
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

do:first_object_by_path

Gets either a value, a name or a generated-id of first node at a specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- first.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xsldo.xsl">
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="first"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="first">
    <xsl:call-template name="do:first_object_by_path">
      <xsl:with-param name="path" select="@path"/>
      <xsl:with-param name="what">text</with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints ABC1 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="first.xsl"?>
<top>
  <first path="//aaa/bbb"/> <!-- Outputs 'ABC1' -->
 
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

do:count_nodes_by_path

Counts elements/attributes at a specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- count.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xsldo.xsl">
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="count"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="count">
    <xsl:call-template name="do:count_nodes_by_path">
      <xsl:with-param name="path" select="@path"/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints 3 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="count.xsl"?>
<top>
  <count path="//aaa/bbb"/> <!-- Outputs '3' -->
 
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

APIs

Since xsldo is a XSL library, this API document can be 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

xsldo is one of libraries in xslet project. xslet is a project to develop Web applications/libraries with XSL (XSLT and XPath).

xsldo depends on following xslet library.

XSLT and XPath is the specifications specified by W3C. xsldo uses XSLT version 1.0 and XPath version 1.0 in it because Web browsers support them.

In addition, xsldo uses XSLT version 3.0 and XPath version 3.0 in the build environment.

The xsldo also uses following application as tools in the build environment.