xslbook-do Ver 0.2

A XSL Library which provides XSLT match templates to operate XML data for xslbook.

Copyright (C) xslet project.
MIT License.

About xslbook-do

xslbook-do is a XSLT library which provides XSLT match templates to operate XML data for xslbook. This library enables to let users to operate XML data without exposing XSLT and XPath specifications to them.

This library implements the match templates to get an object/objects or to loop child nodes. These XSLT templates basically wrap the named templates of xsldo, and the path specifications and so on follow xsldo's specification.

This program is written in XSLT 1.0.

Usage

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

Import xslbook-do

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

  1. Add the namespaces declaration of xslbook, xsldo and xslutil in a XSL file.

    <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"
      xmlns:book="https://github.com/xslet/2020/xslbook"> <!-- This! -->
      ...
  2. Add a import element of xslbook-do in the XSL file.

      ...
      <!-- xslutil.xsl and xsldo.xsl are already imported in xslbook-do.xsl -->
      <xsl:import href="./xslbook-do.xsl"/>
      ...

Call templates

xslbook-do provides the following match templates:

<value of="…"> and <values of="…">

Get a value of first node and values of nodes at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- value.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="value|values|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

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

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="value.xsl"?>
<top>
  <value of="//aaa/bbb"/> and <values of="//aaa/bbb" separator=","/>
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
  <aaa>
    <bbb>ABC3</bbb>
  </aaa>
</top>

<content of="…"> and <contents of="…">

Get a content of first node and contents of nodes at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- content.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="content|contents|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints A1B1 and A1B1,A2B2,A3B3 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="content.xsl"?>
<top>
  <content of="//aaa/bbb"/> and <contents of="//aaa/bbb" separator=","/>
  <aaa>
    <bbb>A1<ccc>B1</ccc></bbb>
    <bbb>A2<ccc>B2</ccc></bbb>
  </aaa>
  <aaa>
    <bbb>A3<ccc>B3</ccc></bbb>
  </aaa>
</top>

<number of="…"> and <numbers of="…">

Get a number of first node and numbers of nodes at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- number.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="number|numbers|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints 11 and 11,NaN,33 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="number.xsl"?>
<top>
  <number of="//aaa/bbb"/> and <numbers of="//aaa/bbb" separator=","/>
  <aaa>
    <bbb>11</bbb>
    <bbb>2A</bbb>
  </aaa>
  <aaa>
    <bbb>33</bbb>
  </aaa>
</top>

<name of="…"> and <names of="…">

Get a name of first node and names of nodes at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- name.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="name|names|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints bbb and bbb,ccc,ddd on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="name.xsl"?>
<top>
  <name of="//aaa/*"/> and <names of="//aaa/*" separator=","/>
  <aaa>
    <bbb>1</bbb>
    <ccc>2</ccc>
  </aaa>
  <aaa>
    <ddd>3</ddd>
  </aaa>
</top>

<preceding_comment of="…"> and <following_comment of="…">

Get a preceding comment of first node and a following comment of first node at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- comment.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="preceding-comment|following-comment|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

By using the above XSL file, the following XML file prints commant1 and comment2 on your Web browser.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="comment.xsl"?>
<top>
  <preceding-comment of="//aaa/bbb"/> and <following-comment of="//aaa/bbb"/>
  <aaa>
    <!--comment1-->
    <bbb>1</bbb>
    <!--comment2-->
  </aaa>
</top>

<count of="…"> and <sum of="…">

Get count of nodes and sum of number values of nodes at the specified path.

<?xml version="1.0" encoding="utf-8"?>
<!-- count_and_sum.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="count|sum|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

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

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="count_and_sum.xsl"?>
<top>
  <count of="//aaa/*"/> and <sum of="//aaa/*"/>
  <aaa>
    <bbb>1</bbb>
    <ccc>2</ccc>
  </aaa>
  <aaa>
    <ddd>3</ddd>
  </aaa>
</top>

<if of="…"> and <choose of="…">

<if> element tests its condition and applies child nodes if the condition is satisfied.
<choose> element finds first <when> element of which condition is satisfied and applies child nodes of the <when> element.

<?xml version="1.0" encoding="utf-8"?>
<!-- if_and_choose.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="if|choose|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

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

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="if_and_choose.xsl"?>
<top>
  <if of="//aaa/@b"/>
    <value of="//aaa/bbb[2]"/>
  </if>
  and
  <choose of="//aaa/@b"/>
    <when eq="1"><value of="//aaa/bbb[1]"/></when>
    <when eq="2"><value of="//aaa/bbb[2]"/></when>
    <otherwise>?</otherwise>
  </choose>
  <aaa b="2">
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
</top>

<for each="…">, <for times="…"> and <index of="…">

<for> element loops to apply child nodes for each nodes at specified path or specified tiems.
<index> element outputs an index of the parent or the ancestor <for> element.

<?xml version="1.0" encoding="utf-8"?>
<!-- for.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:ut="https://github.com/xslet/2020/xslutil"
  xmlns:do="https://github.com/xslet/2020/xsldo"
  xmlns:book="https://github.com/xslet/2020/xslbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="xslbook-do.xsl"/>
  <xsl:template match="/top">
    <html>
      <body>
        <xsl:apply-templates select="for|text()"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

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

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="for.xsl"?>
<top>
  <for index="i" each="//aaa">
    <for each="bbb">
      <index of="i"/>.<index/>.<value/>,
    </for>
  </for>
  and
  <for index="i" times="1">
    <for times="2">
      <index of="i"/>.<index/>.<value>
        <attr name="of">//aaa[<index of="i"/>]/bbb[<index/>]</attr>
      </value>
    </for>
  </for>
  <aaa>
    <bbb>ABC1</bbb>
    <bbb>ABC2</bbb>
  </aaa>
</top>

APIs

Since xslbook-do 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 match/named templates written in the API document.

Links

xslbook-do is one of libraries for xslbook in xslet project. xslbook is a XSLT applet which displays a book-style document on Web browser. xslet is a project to develop Web applications/libraries with XSL (XSLT and XPath).

xslbook-do depends on following xslet library.

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

In addition, xslbook-do uses XSLT version 3.0 and XPath version 3.0 in the build environment.

The xslbook-do also uses following application as tools in the build environment.