http://zorba.io/modules/stack

Description

Before using any of the functions below please remember to import the module namespace:

import module namespace stack = "http://zorba.io/modules/stack";
Implementation of stack for items, using dynamic collections.

Entries in the stack must be structured items - that is, either JSON Objects or Arrays, or XML Nodes.

Module code

Here is the actual XQuery module code.

Imported modules

Authors

Daniel Turcanu, Sorin Nasoi

Version Declaration

xquery version "3.0" encoding "utf-8";

Namespaces

annhttp://zorba.io/annotations
collections-ddlhttp://zorba.io/modules/store/dynamic/collections/ddl
collections-dmlhttp://zorba.io/modules/store/dynamic/collections/dml
stackhttp://zorba.io/modules/stack
verhttp://zorba.io/options/versioning

Variables

stack:errNS

stack:NOT-EXISTS

stack:EXISTS

Function Summary

create($name as xs:QName) as empty-sequence()

Create a stack with this name.

top($name as xs:QName) as structured-item()?

Return the top item in the stack, without removing it.

pop($name as xs:QName) as structured-item()?

Return the top item in the stack, and remove it.

push($name as xs:QName, $value as structured-item()) as empty-sequence()

Add a new item to the stack; the stack will contain a copy of the given item.

empty($name as xs:QName) as xs:boolean

Checks if a stack exists and is empty.

size($name as xs:QName) as xs:integer

Count of items in the stack.

copy($destName as xs:QName, $sourceName as xs:QName) as empty-sequence()

Copy all items from source stack to a destination stack.

Functions

create#1

declare %ann:sequential function stack:create(
    $name as xs:QName
) as empty-sequence()
Create a stack with this name. If stack exists, an error is raised.

Parameters

  • $name

    name of the new stack.

Returns

  • empty-sequence()

    an empty sequence.

Errors

  • stack:EXISTS

    if the stack identified by $name already exists.

top#1

declare function stack:top(
    $name as xs:QName
) as structured-item()?
Return the top item in the stack, without removing it.

Parameters

  • $name

    name of the stack.

Returns

  • structured-item()?

    the top item, or empty sequence if stack is empty.

Errors

  • stack:NOT-EXISTS

    if the stack identified by $name does not exist.

Examples

pop#1

declare %ann:sequential function stack:pop(
    $name as xs:QName
) as structured-item()?
Return the top item in the stack, and remove it.

Parameters

  • $name

    name of the stack.

Returns

  • structured-item()?

    the top item, or empty sequence if stack is empty.

Errors

  • stack:NOT-EXISTS

    if the stack identified by $name does not exist.

Examples

push#2

declare %ann:sequential function stack:push(
    $name as xs:QName,
    $value as structured-item()
) as empty-sequence()
Add a new item to the stack; the stack will contain a copy of the given item.

Parameters

  • $name

    name of the stack.

  • $value

    the item to be added.

Returns

  • empty-sequence()

    an empty sequence.

Errors

  • stack:NOT-EXISTS

    if the stack identified by $name does not exist.

Examples

empty#1

declare function stack:empty(
    $name as xs:QName
) as xs:boolean
Checks if a stack exists and is empty.

Parameters

  • $name

    name of the stack.

Returns

  • xs:boolean

    true if the stack is empty or does not exist.

Errors

  • stack:NOT-EXISTS

    if the stack identified by $name does not exist.

Examples

size#1

declare function stack:size(
    $name as xs:QName
) as xs:integer
Count of items in the stack.

Parameters

  • $name

    name of the stack.

Returns

  • xs:integer

    the amount of items.

Errors

  • stack:NOT-EXISTS

    if the stack identified by $name does not exist.

Examples

copy#2

declare %ann:sequential function stack:copy(
    $destName as xs:QName,
    $sourceName as xs:QName
) as empty-sequence()
Copy all items from source stack to a destination stack.

If destination stack does not exist, it is created first.

If destination stack is not empty, the items are appended on top.

Parameters

  • $destName

    name of the destination stack.

  • $sourceName

    name of the source stack.

Returns

  • empty-sequence()

    an empty sequence.

Examples