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

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

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

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

Mark V. Scardina, Ben ChangandJinyu Wang

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Running the XSLT Virtual Machine withxsbtransform

Now that you have one or more stylesheets and their associated compiled XSB files, you can use them in your applications, as shown in this section as we discuss how xsbtransform works.

To use this resource from an application, you must perform the following steps:



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



Create a parser to read the map file using XMLLoadSax().



Parse the map file for its entries using SAX callbacks.



Create a parser to read the input XML file using XmlLoadDom().



Retrieve the stylesheet PI to get the filename using XmlDomGet*().



Match the XSL filename to its XSB filename.



Create the XSLT virtual machine using XmlXvmCreate().



Perform the transformation by passing in the input XML and XSB file using XmlXvmSetBytecodeFile() and XmlXvmTransformFile().



Get the DOM and output the result of the transformation using XmlXvmGetOutputDom() and XmlSaveDom().

The following code for xsbtransform implements these steps:

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#ifndef XML_ORACLE
#include <xml.h>
#endif
/* Obtains XSL compiled code filename */
oratext *getXSBFileName (xmlctx *xctx, xmldocnode *doc,
oratext *xslmapfile)
{
oratext *p, *filename;
static oratext input[100];
xmlpinode *stpinode, *pinode;
xmlnode *stnode;
filename = (oratext *)malloc(200);
/* Get first PI */
pinode = (xmlpinode *)XmlDomGetFirstChild(xctx, (xmlnode *)doc);
/* Get stylesheet PI */
stpinode = (xmlpinode *)XmlDomGetNextSibling(xctx,
(xmlnode *)pinode);
/* Get href node as last child */
stnode = XmlDomGetLastChild(xctx, (xmlnode *)stpinode);
/* Get attribute value, which is the filename */
filename = XmlDomGetAttrValue(xctx, (xmlattrnode *)stnode);
p = (oratext*)strtok((char*)filename, ".");
strcpy((char*)input, (char*)p);
strcat((char*)input, ".xsb");
return input;
}

The support function getXSBFileName() receives the input XML file DOM and walks the DOM tree using XMLDomGetFirstChild(), XMLDomGetNextSibling(), and XmlDomGetLastChild() to retrieve the stylesheet PI and then calls XmlDomGetAttrValue() to retrieve the stylesheet attribute. Finally, it tokenizes the string to retrieve the filename, less its extension, and returns the compiled stylesheet by appending its extension as follows:

/* ================================================================
main
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
int main (sword argc, char *argv[])
{
xmlctx *xctx = NULL;
xmlxvm *vm = NULL;
xmlerr err;
oratext *xmlFile, *outFile, *xslmapFile, *codeFile;
xmldocnode *doc, *odoc;
xmlfragnode *root;
if (argc < 3) {
printf("Usage is: xsbtransform <xmlfile> <outputfile>
<xslmap file>\n");
return 0;
}
xmlFile = (oratext *)argv[1];
outFile = (oratext *)argv[2];
xslmapFile = (oratext *)argv[3];
/* Get the xsl compiled code */
/* Creates the context */
xctx = XmlCreate(&err, (oratext *) "sample",
"data_encoding", "US-ASCII", NULL);
if (!xctx) {
printf("Failed to create xctx: %d\n", err);
goto terminate;
}
if (!(doc=XmlLoadDom(xctx, &err, xmlFile, NULL)))
{
printf("Parse failed, code %d\n", (int) err);
}
codeFile = getXSBFileName(xctx, doc, xslmapFile);
/* Creates the VM */
vm = XmlXvmCreate (xctx, NULL);
if (!vm) {
printf("Failed to create VM");
goto terminate;
}
/* Create the output document node */
odoc = XmlCreateDocument(xctx, NULL, NULL, NULL, &err);
err = XmlXvmSetOutputDom(vm, odoc);
err = XmlXvmSetBytecodeFile(vm, codeFile);
if (err != XMLERR_OK) {
printf("Failed to set the bytecode: %u\n", err);
goto terminate;
}
err = XmlXvmTransformDom (vm, doc);
if (err != XMLERR_OK) {
printf("Failed to transform: %u\n", err);
goto terminate;
}
/* Get DOM of transformed XML file and write into output file */
root = XmlXvmGetOutputDom(vm);
XmlSaveDom(xctx, &err, root, "outFile", NULL);
terminate:
/* clean the memory */
if (vm)
XmlXvmDestroy(vm);
if (xctx)
XmlDestroy(xctx);
return 0;
}

Once again, the main function is where most of the application work is performed. After the XML context is initialized by XmlCreate(), the input XML file is parsed into a DOM with XmlLoadDom(). At this point you need to retrieve the stylesheet PI, therefore you call the custom getXSBFileName() function, which returns the compiled stylesheet name to use.

The next step initializes the XSLT virtual machine with XmlXvmCreate() and sets it up by setting the output root with XmlCreateDocument() and the output DOM with XmlXvmSetOutputDom(). Finally, the initialization is completed by setting the compiled stylesheet with XmlXvmSetBytecodeFile().

At this point the XSL transformation can take place by calling XmlXvmTransformDom() and passing in the DOM of the input file, doc. You now can get the output document’s DOM by calling XmlXvmGetOutputDom() and print it to a file with XmlSaveDom(). This output method is quite flexible because it can also print to a URI, buffer, stream, or STDIO. You can also specify “pretty printing,” by setting the number of spaces to indent, specifying the output encoding to use, and choosing whether to print the XML declaration.

It is now time to run the program. Make sure that the XML, XSB, and map files are local if specified without paths and then run this example command line:

xsbtransform booklist.xml bookcatalogl xslmap.xml

/ 218