实例简析XPath串函数和XSLT

2008-02-23 05:50:39来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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))

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: .NET中书写XML的一种简单方法

下一篇:asp查询xml的代码实现无刷新 模糊查询