栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > 其他 > xml语言

实例简析XPath串函数和XSLT

xml语言 更新时间:发布时间: 百科书网 趣学号
XPath(xml Path language)是一种处理XML文档段的语言。XSLT(Extensible Stylesheet Language Transformations,可扩展样式表语言转换)使用XPath描述表达式和地址路径控制节点选取。XSLT可以将XML转换为各种格式,如HTML或其他格式。
下面用一个邮件合并程序来简要说明XPath的串函数。下面的XML文件中包含数据,XSLT文件中包含对邮件格式的定义。MSXML4.0对XML文档应用样式表,产生一个合并的邮件文本文档。
XML文件 Letter.xml

  July 17, 2002       Vicky     P     Male    
900 National Pkwy Suite 105 Bellevue WA 98007 USA
ESTATE OF JOHN DOE / FILE NO. 12345.6789 Please pay the PRoperty taxes as soon as possible. John M Sr. Tax Consultant

XSLT样式表文档 Letter.xsl

      To,Regarding: Dear Mr. Miss ,Sincerely,

上面的样式表举例说明了concat和starts-with XPath串函数和怎样在输出文本中增加新行,还有定义和使用变量。
下面是程序的执行结果。

1.VC6建立Win32控制台应用程序。
2.在stdafx.h中添加下面的代码:

#include #include #include #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 // 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 bufferstatic _TCHAR   szTemp[TEMP_SIZE];         // multipurpose buffer on stackstatic 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, "nnPress Enter to continue..."); getchar(); CoUninitialize(); return 0;}

5.XML文件和XSLT样式表文件名作为命令行参数传递给应用程序。主函数通过调用isMSXMLInstalled验证 MSXML4.0是否安装。接下来两次调用loaddocument;先是加载XML文档,然后是加载XSLT样式表。 最后调用transformNode进行转换。

6.本例代码下载:http://www.perfectxml.com/CPPMSXML/downloads/20020716MailMerge.zip

以上就是实例简析XPath串函数和XSLT的内容,更多相关内容请关注PHP中文网(www.iotsi.net)!

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/794740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号