• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 网页制作 > XML/XSLT >

实例简析XPath串函数和XSLT

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-04-15点击:
 xpath(xml path language)是一种处理xml文档段的语言。xslt(extensible stylesheet language transformations,可扩展样式表语言转换)使用xpath描述表达式和地址路径控制节点选取。xslt可以将xml转换为各种格式,如html或其他格式。 
    下面用一个邮件合并程序来简要说明xpath的串函数。下面的xml文件中包含数据,xslt文件中包含对邮件格式的定义。msxml4.0对xml文档应用样式表,产生一个合并的邮件文本文档。
xml文件 letter.xml
<?xml version="1.0" encoding="utf-8"?>
<letter>
  <date>july 17, 2002</date>
  <to>
     <firstname>vicky</firstname>
     <lastname>p</lastname>
     <sex>male</sex>
  </to>
  <address>
     <line1>900 national pkwy</line1>
     <line2>suite 105</line2>
     <city>bellevue</city>
     <state>wa</state>
     <zip>98007</zip>
     <country>usa</country>
  </address>
  <subject>estate of john doe / file no. 12345.6789</subject>
  <text>
     please pay the property taxes as soon as possible.
  </text>
  <sender>
     <firstname>john</firstname>
     <lastname>m</lastname>
     <title>sr. tax consultant</title>
  </sender>
</letter>
xslt样式表文档 letter.xsl
<?xml version='1.0' encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:output method="text" indent="yes"/>
<xsl:variable name="nl" select="'&#xa;'" />
<xsl:variable name="para" select="concat($nl, $nl)" />
<xsl:template match="/">
   <xsl:value-of select="//date" />
   <xsl:value-of select="$para" />
to,
<xsl:value-of select="concat(//to/firstname, ' ', //to/lastname)" />
<xsl:value-of select="$nl" />
<xsl:value-of select="//address/line1" />
<xsl:value-of select="$nl" />
<xsl:value-of select="//address/line2" />
<xsl:value-of select="$nl" />
<xsl:value-of select="concat(//address/city, ' ', //address/state, ' ', //address/zip)" />
<xsl:value-of select="$nl" />
<xsl:value-of select="//address/country" />
<xsl:value-of select="$para" />
regarding: <xsl:value-of select="//subject" />
<xsl:value-of select="$nl" />
dear <xsl:if test="starts-with(//sex, 'm')">mr. </xsl:if><xsl:if test="starts-with(//sex, 'f')">miss </xsl:if>
<xsl:value-of select="concat(//to/firstname, ' ', //to/lastname)" />,
<xsl:value-of select="$para" />
<xsl:value-of select="//text" />
<xsl:value-of select="$para" />
sincerely,
<xsl:value-of select="$para" />
<xsl:value-of select="concat(//sender/firstname, ' ', //sender/lastname)" />
<xsl:value-of select="$nl" />
<xsl:value-of select="//sender/title" />
</xsl:template>
</xsl:stylesheet>
  
    上面的样式表举例说明了concat和starts-with xpath串函数和怎样在输出文本中增加新行,还有定义和使用变量。
    下面是程序的执行结果。
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进行转换。
标签: xpath串函数

猜您也喜欢...

猜你也喜欢看这些...

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1170734538@qq.com ,  QQ