Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Compiling Stylesheets with xslcompile

Because the Oracle XSLT Compiler resides in oraxml10.lib on Windows NT and libxml10.a, it can be used either as a stand-alone application or from within another application. Using the compiler from within an application requires these steps:



Create an instance of the XDK context using XmlCreate().



Create an instance of the XSLT compiler using XmlXvmCreateComp().



Compile the XSLT file using XmlXvmCompileFile().

At this point, you can either pass the compiled stylesheet to the VM or, as you will do in this application, write it to a file. The following is the code for xslcompile:

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifndef XML_ORACLE
# include <xml.h>
#endif
/* Deposit xsb and insert into the map file function */
void depositxsb(char *fileargv, ub2 *code, ub4 len, char *filemap)
{
char filename[200], *name;
char *input, *inputxsl;
FILE *xsb;
xmlctx *ctx;
xmlerr *ecode;
xmltextnode *textnode;
xmlattrnode *attr1;
xmldocnode *doc;
xmlelemnode *node, *elem;
name = (char *)strtok(fileargv, ".");
strcpy(filename, name);
strcat(filename, ".xsb");
xsb = fopen(filename, "w");
fwrite((void *)code, len, 1, xsb);
fclose(xsb);
/* Assuming the XML declaration and root node already exist
* in the map file. Init and deal with xml map file
*/
if (!(ctx = XmlCreate(ecode, (oratext *)"xml_context", NULL)))
{
printf("Failed to initialize XML parser, error \n");
}
if (!(doc=XmlLoadDom(ctx, ecode, filemap, NULL)))
{
printf("Parse failed, code %d\n", (int) ecode);
}
/* Get root element */
node = XmlDomGetDocElem(ctx, doc);
/* Create the element, add data, create the attribute */
if (!(elem = XmlDomCreateElem(ctx, doc, (oratext *)"xsl")))
{
printf("Element creation failed\n");
}
if (!(textnode = XmlDomCreateText(ctx, doc, (oratext *)inputxsl)) ||
!XmlDomAppendChild(ctx, elem, textnode))
{
printf("Text node creation failed\n");
}
if (!(attr1 = XmlDomCreateAttr(ctx, doc, (oratext*)"xsb",
(oratext*)input)))
{
printf("Attribute creation failed\n");
}
if (!XmlDomSetAttrNode(ctx, elem, attr1))
{
printf("Attribute setting failed\n");
}
/* insert the element */
if (!XmlDomAppendChild(ctx, (xmlnode *)node, (xmlnode *)elem))
{
printf("Appending to root element failed \n");
}
XmlFreeDocument(ctx, doc);
}

The depositxsb() function performs three supporting tasks. First, it creates the file to which the compiled stylesheet will be saved. Second, it updates the xslmap.xml file with an entry for the compiled stylesheet and associates it with the actual stylesheet that will be in the input XML document PI. Finally, it cleans up after itself before returning to the main function, which follows:

/* ====================================================================
main
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
int main (sword argc, char *argv[])
{
xmlctx *xctx = NULL;
xmlxvmcomp *comp = NULL;
xmlerr err;
ub2 *code;
ub4 len;
oratext *xslFile;
if (argc < 2) {
printf("Usage is: xslcompile <input xsl>\n");
return -1;
}
xslFile = (oratext*)argv[1];
xctx = XmlCreate(&err, (oratext *) "sample",
"data_encoding", "US-ASCII", NULL);
if (!xctx) {
printf("Failed to create xctx: %d\n",err);
goto terminate;
}
comp = XmlXvmCreateComp (xctx);
if (!comp) {
printf("Failed to create Compiler.");
goto terminate;
}
code = XmlXvmCompileFile (comp, xslFile, NULL, 0, &err);
if (err != XMLERR_OK) {
printf("Failed to Compile:%u\n", err);
goto terminate;
}
len = XmlXvmGetBytecodeLength(code, &err);
/* Deposit in xsb */
depositxsb(argv[1], code, len, argv[2]);
terminate:
/* clean the memory */
if (comp)
XmlXvmDestroyComp(comp);
if (xctx)
XmlDestroy(xctx);
return 0;
}

The preceding is the main routine in the program. It first initializes the XML context, xctx, which is the necessary first step for all C XDK applications. To compile stylesheets, you must first initialize the XSLT compiler, which is done using XmlXvmCreateComp() and passing in your XML context. Now you can compile by calling XmlXvmCompileFile() and passing in the stylesheet file. Note that this function supports passing in a file path and/or base URI in order to locate the stylesheet. For this example, since the file is local, the base URI in not used.

Once you have the compiled stylesheet, all that is left is to get its length, since it is a binary file, and pass it to depositxsb() along with the map filename to have it saved and to have the map file updated.

Once xslcompile is compiled and linked, you can execute it with the following command line and any supported XSLT file:

xslcompile bookcatalog.xsl xslmap.xml

Note that the Oracle XSLT Compiler supports only XSLT 1.0 and XPath 1.0 in the Oracle XDK 10g release and thus the stylesheets must be limited to this version.

/ 218