实例简析XPath串函数和XSLT(3)
来源:互联网 时间:2008-09-04 14:16:00
下面是程序的执行结果。
1.VC6建立Win32控制台应用程序。
2.在stdafx.h中添加下面的代码:
#include <TCHAR.H>
#include <stdio.h>
#include <time.h>
#import "msxml4.dll"
// If this import statement fails, you need to install MSXML 4.0 SP1 from:
//http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/001/766/msdncompositedoc.xml
#include <msxml2.h>
// If this include statement fails, you need to install MSXML 4.0 SP1 SDK from:
//http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/001/766/msdncompositedoc.xml
// You also need to add the include file and library search path
// to Visual C++'s list of directories (Tools > Options... > Directories).
using namespace MSXML2;
inline void EVAL_HR( HRESULT _hr )
{ if FAILED(_hr) throw(_hr); }
#define TEMP_SIZE _MAX_PATH // size of short buffer
static _TCHAR szTemp[TEMP_SIZE]; // multipurpose buffer on stack
static DWORD dwLen;
3.上面的代码引入MSXML4类型库,包含MSXML头文件,检查HRESULT值并声明了一些全局变量。
4.main函数:
int main(int argc, char* argv[])
{
try
{
EVAL_HR(CoInitialize(NULL));
// Make sure that MSXML 4.0 is installed
if (!isMSXMLInstalled())
return -1;
// Make sure that XML and XSL file names are passed
// as command line parameters
if (argc < 3)
// Show proper message here
return -1;
IXMLDOMDocument2Ptr pXMLDoc = NULL;
IXMLDOMDocument2Ptr pXSLDoc = NULL;
// Load the XML document
if (loadDocument(pXMLDoc, argv[1], true))
{
// Load the stylesheet
if (loadDocument(pXSLDoc, argv[2], false))
{
_ftprintf(stdout, pXMLDoc->transformNode(pXSLDoc));
}
else
{
printMSXMLError(pXSLDoc);
}
}
else
{
printMSXMLError(pXMLDoc);
}
}
catch(...)
{//exception handling
}
_ftprintf(stdout, "\n\nPress Enter to continue...");
getchar();
CoUninitialize();
return 0;
}
5.XML文件和XSLT样式表文件名作为命令行参数传递给应用程序。主函数通过调用isMSXMLInstalled验证 MSXML4.0是否安装。接下来两次调用loadDocument;先是加载XML文档,然后是加载XSLT样式表。 最后调用transformNode进行转换。