6. Built-in commands

This paragraph describes the built-in commands that come standard with xpt.

6.1. XML bindings

This paragraph describes the built-in commands that can be used with processing-instructions in the input XML stream. User-defined commands can be created from the embedded Python engine (http://www.python.org/). Most commands can have arguments. There are two types of arguments: positional and named arguments. EXPLAIN. Then there is quoting: unquoted, single-quoted and double-quoted. Optionally glueing those parts together. Note that built-in commands (currently?) have no named arguments, only positional. See the section on variable substitution Section 6.2, “Variable substitution”.

6.1.1. code

code NEWLINE CODE

Insert a block of executable code. Note that a NEWLINE must be after the code keyword. From that point the text is passed to the Python interpreter. The node wil be removed from the output.

Example 1. example built-in code usage

<example-code>
<?xpt code
import libxml2
import xpt

def myFunction(nd,arg1):
    return "myFunction: nd="+nd.name+" arg1='"+arg1+"'"

xpt.registerfunction("myFunction")?></example-code>

<example-call>
<?xpt myFunction "an argument"?>
</example-call>


In the example above, the first processing-instruction using the code statement registers a new xpt function called myFunction which is associated with the Python function with the same name. After evaluation it will be removed from the output. In the second processing-instruction the xpt function is evaluated. It will be replaced with the result returned by the Python function, in example Example 1, “example built-in code usage” that is the string "myFunction: nd=xpt arg1='an argument'".

In Python a module variable __xpt_code_xmlNode__ is set to the libXML2 node containing the current code statement. The variable is removed when the execution of the module stops.

[Note]Note

All Python functions that are called from xpt have at least one argument, i.e. a xmlNode pointer referencing the XML element from where the actual call originates. In this is the example-call element.

See also Section 6.1.6, “loadmodule”. xpt.debug

6.1.2. dumpdoc

__dumpdoc {STRING}

Debugging command which displays the current document to the standard error output, using STRING as a message. Note that debugging output must be enabled by using option -x or environment variable.

6.1.3. dumpvars

__dumpvars {STRING}

Debugging command which shows a list of currently defined variables to the standard error output, using STRING as a message. Note that debugging output must be enabled by using option -x or environment variable.

6.1.4. echo

echo {STRING}

Evaluate STRING and replace the processing-instruction by the string representation.

See also Section 6.1.8, “get”.

6.1.5. error

error {STRING}

Evaluate STRING, report it on the standard error channel and stop processing of the input. No output file will be written. The xpt process returns an exit-code of 1 to the parent process.

6.1.6. loadmodule

loadmodule MODULE

Load Python module MODULE. The node wil be removed from the output.

Example 2. example built-in loadmodule usage

<?xpt showNextPI?>

See also ???.

6.1.7. python

python {python-expression}

Replace the processing-instruction with the result of the python-expression.

6.1.8. get

get {NAME}

Replace the processing-instruction with the value of variable NAME.

Example 3. example built-in get usage

<?xpt get __hostname__?>

Evaluation of the processing-instruction above gives: pluto


Here See also Section 6.1.4, “echo”.

6.1.9. select

select [NAME] {XPATH}

Replace the processing-instruction with the result of the XPATH expression. If NAME is given the result is saved in a variable and the processing-instruction is removed from the output.

6.1.10. set

set NAME VALUE

Create or replace variable NAME with value VALUE. The node wil be removed from the output.

Example 4. example built-in set usage

<?xpt set var1 "**** This is var1 ****"?>

Evaluation of the processing-instruction above gives:


In the example above, var1 will have value **** This is var1 ****.

6.1.11. setmatch

CASE-like expression

6.1.12. shell

shell CMD

Replace the processing-instruction with the value of the shell command CMD.

6.1.13. warning

warning MSG

Evaluate string MSG and send it to the standard error output. The processing-instruction is removed from the output.

6.2. Variable substitution

Variable substitution: replace ${NAME} constructions with the value associated the the variable NAME. There are shell-like quoting rules: single and double quotes have different effect.

6.3. Python bindings

This paragraph describes the XPT bindings for the Python environment.

6.3.1. xpt.debug

import xpt
None fsfuncxpt.debug(); 
message;
 

Issue a message on the debug output channel. See -x.

6.3.2. xpt.dumpvars

import xpt
None fsfuncxpt.dumpvars(); 
message;
 

Write list of XPT variables on the debug output channel. See -x and Section 6.1.3, “dumpvars”.

6.3.3. xpt.error

import xpt
None fsfuncxpt.error(); 
message;
 

Write a message to the standard error channel and stop processing of the input. See Section 6.1.5, “error”.

6.3.4. xpt.get

Get value of a variable. An exception is raised if the variable does not exist.

6.3.5. xpt.registerfunction

import xpt
None fsfuncxpt.registerfunction(,  
 ); 
name;
func;
 

Registers xpt function name which is associated with the Python function func. If this second parameter is not specified, it's name is assumed to be identical to name prefixed with the name of the module from which the registerfunction is called.

6.3.6. xpt.set

6.3.7. xpt.warning

import xpt
None fsfuncxpt.warning(); 
message;
 

Write a message to the standard error channel. See Section 6.1.13, “warning”.

6.4. XPT library

Xpt comes with extra functionality through a library of functions.

6.4.1. xpt.docbook5 - DocBook 5 functions

6.4.2. xpt.csv - CSV functions

6.4.3. Extending xpt

There are several way of extending xpt. First by pointing xpt where the Python modules are using the -I option. A second way is by extending the library, giving you an opportunity to enter the XPT Hall of Fame.