相关官方文档:
http://xmlsoft.org/XSLT/tutorial/libxslttutorial.html
下文引用自:
http://blog.csdn.net/ThinkHY/archive/2009/02/26/3940322.aspx
STEP1 : 在这个网址: http://xmlsoft.org/sources/win32/
下载这几个包:
将lib,dll,include文件分别拷贝到合适的位置。
STEP2:
编写测试代码:
XML Schema:
#include "libxml/tree.h" #include "libxml/parser.h" #include "libxml/xmlschemas.h" /**************************************************** @describle 应用XML Schema模板文件验证案例文档 @param schema_filename 模式文件 @param xmldoc XML格式的案例文档 @retval >=0 验证成功 < 0 验证失败 ****************************************************/ int is_valid(const char *schema_filename, const char *xmldoc) { xmlDocPtr doc; doc = xmlReadFile(xmldoc, NULL, XML_PARSE_NONET|XML_PARSE_NOENT); if ( NULL == doc) { MessageBox(NULL,"读取XML文档错误",NULL,NULL); return -1; } xmlDocPtr schema_doc = xmlReadFile(schema_filename, NULL, XML_PARSE_NONET|XML_PARSE_NOENT); if ( NULL == schema_doc) { MessageBox(NULL,"读取Schema错误",NULL,NULL); return -1; } xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc); if (NULL == parser_ctxt) { return -1; } xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { return -1; } xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); if (NULL == valid_ctxt) { return -1; } int is_valid = (xmlSchemaValidateDoc(valid_ctxt,doc) == 0); xmlSchemaFreeValidCtxt(valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); return is_valid; }
XSLT:
#include "libxslt/xslt.h" #include "libxslt/xsltInternals.h" #include "libxslt/transform.h" #include "libxslt/xsltutils.h" /**************************************************** @describle 应用XSLT模板转换案例格式 @param stylesheet XSLT模板文件 @param olddoc 待转换案例文件 @param newdoc 转换后的案例文件 @retval >=0 转换成功 < 0 转换失败 ****************************************************/ int xslt(const char *stylesheet, const char *olddoc, const char *newdoc) { const char *params[16+1]; int ret; xsltStylesheetPtr cur = NULL; xmlDocPtr doc, res; FILE *outfp; /*设置参数*/ params[0] = NULL; /*设置libxml*/ xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; cur = xsltParseStylesheetFile((const xmlChar *)stylesheet); if ( NULL == cur ) { MessageBox(NULL,"分析XSLT模板文件出错",NULL,NULL); return -1; } doc = xmlParseFile(olddoc); res = xsltApplyStylesheet(cur, doc, (const char **)params); if ( NULL == res ) { MessageBox(NULL,"应用模板出错",NULL,NULL); return -1; } if (NULL == res->children) { MessageBox(NULL,"res->children为NULL",NULL,NULL); return -1; } ret = xsltSaveResultToFilename(newdoc,res,cur,0); xsltFreeStylesheet(cur); xmlFreeDoc(res); xmlFreeDoc(doc); xsltCleanupGlobals(); xmlCleanupParser(); return ret; }