[All Packages] [Previous]
A software module called an XSL processor is used to read XML documents and transform them into other XML documents with different styles.
The C implementation of the XSL processor follows the XSL Transformations standard (version 1.0, November 16, 1999) and implements the required behavior of an XSL processor as specified in the XSLT specification.
/* parse XML file */ xmlctx = xmlinitenc (...); ecode = xmlparse (xmlctx, xmlFile, ...); ... /* parse XSL file */ xslctx = xmlinitenc (...); ecode = xmlparse (xslctx, xslFile, ...); ... /* create result XML context */ resctx = xmlinitenc (...); ... /* XSLT processing */ ecode = xslprocess (xmlctx, xslctx, resctx, ...);Typical calling sequence using xmlparsexsl:
/* parse XML file */ xmlctx = xmlinitenc (...); ecode = xmlparse (xmlctx, xmlFile, ...); ... /* parse XSL file */ xslctx = xmlinitenc (...); ecode = xmlparse (xslctx, xslFile, ...); ... /* create result XML context */ resctx = xmlinitenc (...); ... /* create and initialize XSL context */ xslSSctx = xslinit (..., xslctx, ...); ecode = xslsetoutputmethod (xslSSctx, ...); ecode = xslsetoutputdomctx (xslSSctx, resctx); ecode = xslsettextparam (xslSSctx, ...); ... /* XSLT processing */ ecode = xslprocessxml (xslSSctx, xmlctx, ...);
| oratext | String pointer used for all data encodings, cast as needed; for UTF-16, to (ub2 *) |
| xmlctx | Top-level XML context |
| xmlsaxcb | SAX callback structure (SAX only) |
| xmlnode | Document node |
| xmlstream | Stream based output |
| xslctx | Top-level XSL context |
| xsloutputmethod | Output method for XSL processing |
| xpnset | XPath node set |
| xpnsetele | XPath node set element |
| xpobj | XPath object |
| xpobjtyp | XPath object type |
| ub4 | 32-bit (or larger) unsigned integer |
| uword | Native unsigned integer |
XSLT | |
| xslgetbaseuri | Get baseURI associated with the XSLT context. |
| xslgetoutputdomctx | Get result XML context. |
| xslgetoutputsax | Get the output SAX object. |
| xslgetoutputstream | Get the output XML stream. |
| xslgetresultdocfrag | Get the resultant doc fragment (after xslprocessxml). |
| xslgettextparam | Get a toplevel parameter/variable. |
| xslgetxslctx | Get the XML context for the input XSL stylesheet. |
| xslinit | Creates and initializes XSLT context. |
| xslprocess | Processes XSL Stylesheet with XML document source. |
| xslprocessex | Processes XSL Stylesheet with XML document source, extended. |
| xslprocessxml | Do XSLT processing on a given input XML tree. |
| xslprocessdocfrag | Do XSLT processing on a given input XML fragment. |
| xslresetallparams | Resets all the top level parameters added. |
| xslsetoutputdomctx | Set XML context for the output DOM tree. |
| xslsetoutputencoding | Set XSLT output encoding. |
| xslsetoutputmethod | Set XSLT output method. |
| xslsetoutputsax | Set the XSLT output SAX callback object. |
| xslsetoutputsaxctx | Set the XSLT output SAX context. |
| xslsetoutputstream | Set the XSLT output stream callback object. |
| xslsettextparam | Set a toplevel parameter. |
| xslterm | Terminates an XSLT context. |
XPath | |
| xpevalxpathexpr | Evaluates XPath expression |
| xpfreexpathctx | Frees XPath context |
| xpgetbooleanval | Returns value of XPath expression |
| xpgetfirstnsetelem | Returns value of XPath expression |
| xpgetnextnsetelem | Returns value of XPath expression |
| xpgetnsetelemnode | Returns value of XPath expression |
| xpgetnsetval | Returns value of XPath expression |
| xpgetnumval | Returns value of XPath expression |
| xpgetrtfragval | Returns value of XPath expression |
| xpgetstrval | Returns value of XPath expression |
| xpgetxpobjtyp | Returns value of XPath expression |
| xpmakexpathctx | Creates an XPath contex. |
| xpparsexpathexpr | Parses an XPath expression |
typedef unsigned char oratext;
typedef struct xmlctx xmlctx;
Note: The contents of xmlctx are private and must not be accessed by users.
typedef struct xmlnode xmlnode;
Note: The contents of xmlnode are private and must not be accessed by users.
struct xmlstream;
typedef struct xmlstream xmlstream;
#define XMLSTREAM_OPENF(func) uword func(xmlstream *stream, ub4 rsvd)
#define XMLSTREAM_WRITEF(func) uword func(xmlstream *stream, size_t len, \
oratext *buf, size_t *lenwritten, \
ub4 rsvd)
#define XMLSTREAM_CLOSEF(func) uword func(xmlstream *stream, ub4 rsvd)
struct xmlstream {
XMLSTREAM_OPENF((*open));
XMLSTREAM_WRITEF((*write));
XMLSTREAM_CLOSEF((*close));
/* User can associate any data to lpxstream structure using this member
so that he can access that inside the callbacks which will always
have xmlstream as the first argument.
*/
void *userdata;
};
struct xmlsaxcb
{
sword (*startDocument)(void *ctx);
sword (*endDocument)(void *ctx);
sword (*startElement)(void *ctx, const oratext *name,
const struct xmlattrs *attrs);
sword (*endElement)(void *ctx, const oratext *name);
sword (*characters)(void *ctx, const oratext *ch, size_t len);
sword (*ignorableWhitespace)(void *ctx, const oratext *ch,
size_t len);
sword (*processingInstruction)(void *ctx, const oratext *target,
const oratext *data);
sword (*notationDecl)(void *ctx, const oratext *name,
const oratext *publicId,
const oratext *systemId);
sword (*unparsedEntityDecl)(void *ctx, const oratext *name,
const oratext *publicId,
const oratext *systemId,
const oratext *notationName);
sword (*nsStartElement)(void *ctx, const oratext *qname,
const oratext *local,
const oratext *namespace,
const struct xmlattrs *attrs);
sword (*comment)(void *ctx, const oratext *data);
sword (*elementDecl)(void *ctx, const oratext *name,
const oratext *content);
sword (*attributeDecl)(void *ctx, const oratext *elem,
const oratext *attr, const oratext *body);
/* The following 5 fields are reserved for future use. */
void (*empty1)();
void (*empty2)();
void (*empty3)();
void (*empty4)();
void (*empty5)();
};
typedef struct xmlsaxcb xmlsaxcb;
Note: Callbacks for SAX-like API.
typedef struct xslctx xslctx;
Note: The contents of xslctx are private and must not be accessed by users.
typedef enum {
XSLMETHOD_UNKNOWN = 0,
XSLMETHOD_XML,
XSLMETHOD_TEXT,
XSLMETHOD_HTML
} xsloutputmethod;
typedef struct xpobj xpobj;
Note: The contents of xpobj are private and must not be accessed by users.
typedef enum xpobjtyp {
XPOBJTYP_BOOL, /* boolean */
XPOBJTYP_NUM, /* number */
XPOBJTYP_STR, /* string */
XPOBJTYP_NSET, /* node set */
XPOBJTYP_RTFRAG /* result tree fragment */
} xpobjtyp;
typedef struct xpnset xpnset;
Note: The contents of xpnset are private and must not be accessed by users.
typedef struct xpnsetele xpnsetele;
Note: The contents of xpnsetele are private and must not be accessed by users.
typedef unsigned int ub4;
typedef unsigned int uword;
uword xslprocess (xmlctx *docctx, xmlctx *xslctx, xmlctx *resctx,
xmlnode **result);
uword xslprocessex (xmlctx *docctx, xmlctx *sctx, xmlctx *resctx,
size_t nparams, oratext *params[], oratext *vals[],
oratext *baseuri, xmlnode **result);
docctx (IN/OUT) - The XML document context xslctx (IN) - The XSL stylesheet context resctx (IN) - The result document fragment context result (IN/OUT) - The result document fragment node nparams (IN) - Number of (params,vals) pairs to be passed params (IN) - Array of name of pars (must be nparams in count) vals (IN) - Array of value of pars (must be nparams in count) baseuri (IN) - String giving the base uri for style sheet
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
uword xslprocessxml (xslctx *xslSSctx, xmlctx *docctx,
boolean normalize, ub4 resvd);
uword xslprocessxmldocfrag (xslctx *xslSSctx, xmlctx *docctx,
xmlnode *docFrag,
boolean normalize, ub4 resvd);
docctx (IN/OUT) - The XML document context xslSSctx (IN) - The XSL processing context docFrag (IN) - The input document fragment node normalize(IN) - If TRUE then noramlize the input DOM tree. resvd (--) - Reserved. Should be set to NULL.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
Those XSLT API are supposed to be used after the user:
(1) has created the XSLT processing context using xslinit,
(2) has set the base URI using xslinit,
(3) has set all the top level parameters using xslsettextparam,
(4) has selected an output method using xslsetoutput.
The functions will process the given XML file (specified by docctx)
according to the XSL stylesheet, parameters, and output method.
This call returns one of the following depending on the output scheme which
the user might have selected:
Stream based output: The output text has been redirected
using the stream callbacks.
SAX based output: User should have already got all
the SAX events sent by XSLT.
DOM based output: User can make a call to the xslgetresultdocfrag
function to access the final Document Fragment
created in context of the resctx set by a call
to xslsetoutputdomctx.
xslctx* xslinit (uword *err, xmlctx *xsctx,
const oratext *baseURI, ub4 resvd);
err (OUT) - LPXERR_OK on success, otherwise error code. xslctx (IN) - The XML context for the input stylesheet baseURI(IN) - The base URI for processing include and import statements rsvd (--) - reserved for future usage, presently should be set NULL
The pointer to the XSL context object if successful, otherwise NULL.
const oratext* xslgetbaseuri (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the base URI if successful, otherwise NULL.
xmlctx* xslgetoutputdomctx (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the output DOM context object if successful, otherwise NULL.
xmlsaxcb* xslgetoutputsax (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the output SAX object if successful, otherwise NULL.
xmlstream* xslgetoutputstream (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the output stream object if successful, otherwise NULL.
xmlnode* xslgetresultdocfrag (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the document fragment node if successful, otherwise NULL.
xmlctx* xslgetxslctx (xslctx *xslSSctx);
xslSSctx (IN) - the XSL processing context to be inquired upon
The pointer to the XML context object of the XSL stylesheet if successful,otherwise NULL.
uword xslsettextparam (xslctx *xslSSctx, const oratext *param_name,
const oratext *param_value);
xslSSctx (IN) - The XSL processing context param_name (IN) - The name of parameter(variable) whose value is to be set. param_val (IN) - The parameter value
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
const oratext *xslgettextparam (xslctx *xslSSctx, const oratext *param_name);
xslSSctx (IN) - The XSL processing context param_name (IN) - The name of parameter(variable) whose value is to be set.
The pointer to the variable/parameter value if successful, otherwise NULL.
uword xslsetoutputdomctx (xslctx *xslSSctx, xmlctx *resctx);
xslSSctx (IN) - The XSL processing context
resctx (IN) - The XML context object in which the output document
fragment will be stored. This pointer must remain valid
through out the duration when this context is used to
generate the processing event during subsequent
processing of XML trees.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
A call to xslsetoutputdomctx overrides any previous output method set using any previous calls to xslsetoutputdomctx or xslsetoutputstream or xslsetoutputsax. Any subsequent processing of the XML input will be stored in form of a Document Fragment in the xmlctx specified by resctx. xslgetoutputdomctx can be used to retrieve the presently set resctx. If no DOM context is set or a call to xslsetoutputdomctx has been overridden by a subsequent call to xslsetoutputstream or xslsetoutputsax, then xslgetoutputdomctx will return a NULL context indicating that the present output mode is not a DOM output mode.
uword xslsetoutputencoding (xslctx *xslSSctx, oratext* outcoding);
xslSSctx (IN) - The XSL processing context outcoding (IN) - Encoding name for the output encoding.
LPXERR_OK on success, otherwise returns LPXERR_BAD_ENCODING.
This call is used to set an output method equivalent to XSL processing directive xsl:output encoding = ".....". If the xsl stylesheet contains one or more similar statements, then this function will override the effect of any previously present statements in XSL stylesheet.
uword xslsetoutputmethod (xslctx *xslSSctx, xsloutputmethod method, ub4 resvd);
xslSSctx (IN) - The XSL processing context. method (IN) - Output method to be selected. resvd (--) - Reserved for future, user must set it to NULL.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
This call is used to set an output method equivalent to XSL processing directive xsl:output method = "text/xml/html". If the xsl stylesheet contains one or more similar statements, then this function will override the effect of any previously present statements in XSL stylesheet.
uword xslsetoutputsax (xslctx *xslSSctx, xmlsaxcb *s); uword xslsetoutputsaxctx (xslctx *xslSSctx, void *saxctx);
xslSSctx (IN) - The XSL processing context. s (IN) - The SAX callbacks to be used to generate the SAX events. saxctx (IN) - The SAX context.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
The user can make a call to xslsetoutputsaxctx to set the SAX context to be used by the SAX callbacks. If none is set, then a NULL will be passed to the SAX callbacks as SAX context. Also note that just setting the callbacks is sufficient to invoke the SAX callbacks.
uword xslsetoutputstream (xslctx *xslSSctx, xmlstream * stream);
xslSSctx (IN) - The XSL processing context. stream (IN) - The stream structure giving the callbacks to be used.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
uword xslresetallparams (xslctx *xslSSctx);
xslSSctx (IN) - The XSL processing context
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
uword xslterm (xslctx *xslSSctx);
xslSSctx (IN) - The XSL processing context.
LPXERR_OK on success, otherwise returns LPXERR_FAIL.
xpctx *xpmakexpathctx (xmlctx *ctx, xmlnode *xslnode,
xmlnode* xml_node, oratext* baseURI,
size_t nctxels, xmlnode ** ctxnodes);
sctx (IN) - XSL context. Could be NULL.
xslnode (IN) - The XSL node to be used for namespace expansion.
Could be set to NULL.
xmlnode (IN) - The context node. Set to NULL for parsing.
baseURI (IN) - The base URI for parsing.
nctxels (IN) - The number of nodes in the current node set.
ctxnodes (IN) - The current node set.
An XPath context object. This call never fails.
void xpfreexpathctx (xpctx *xctx);
xctx (IN) - The XPath context to be freed.
xpexpr *xpparsexpathexpr (xpctx *xctx, oratext *expr, sword *err);
xctx (IN/OUT) - The xpath context expr (IN) - The expression in the form of a string. err (OUT) - The error code.
On success, returns the expression tree. On failure, returns NULL.
xpobj *xpevalxpathexpr (xpctx *xctx, xpexpr *exprtree, sword *err);
xctx (IN/OUT) - The xpath context exprtree (IN) - The expression in the form of a tree. err (OUT) - The error code.
On success, returns the result object. On failure, returns NULL.
The type of the result object is one of the following:XPOBJTYP_BOOL XPOBJTYP_NUM XPOBJTYP_STR XPOBJTYP_NSET XPOBJTYP_RTFRAG
The object type can be obtained using xpgetxpobjtyp.
xpobjtyp xpgetxpobjtyp (xpobj *xobj); boolean xpgetbooleanval (xpobj *xobj); double xpgetnumval (xpobj *xobj); oratext* xpgetstrval (xpobj *xobj); xpnset* xpgetnsetval (xpobj *xobj); xmlnode* xpgetrtfragval (xpobj *xobj); xpnsetele* xpgetfirstnsetelem (xpnset *nset); xpnsetele* xpgetnextnsetelem (xpnsetele *nsetele); xmlnode* xpgetnsetelemnode (xpnsetele *nsetele);
xobj (IN) - The expression object nset (IN) - The node set. nsetele (IN) - The node set element.
On success, returns the expression value. On failure, returns NULL.
...
xctx = xpmakexpathctx(...);
...
exprtree = xpparsexpathexpr(xctx, ...);
...
xobj = xpevalxpathexpr(xctx, exprtree, ...);
...
switch (xpgetxpobjtyp(xobj))
{
case XPOBJTYP_BOOL:
bval = xpgetbooleanval(xobj); /* bval is of type boolean */
... Use bval;
break;
case XPOBJTYP_NUM:
nval = xpgetnumval(xobj); /* nval is of type double */
... Use nval;
break;
case XPOBJTYP_STR:
strval = xpgetstrval(xobj); /* strval is of type oratext */
... Use strval;
break;
case XPOBJTYP_NSET:
nsetval = xpgetnsetval(xobj);
... Use nset as follws: nsetele is of type xpnsetele*
for (nsetele = xpgetfirstnsetelem(nsetval);
nsetelem;
nsetelem = xpgetnextnsetelem(nsetval))
{
node = xpgetnsetelemnode(nsetele);
... Use node
}
break;
case XPOBJTYP_RTFRAG:
node = xpgetrtfragval(xobj); /* node is of type xmlnode */
break;
}