Function
XPath (XML Path language) is a language that processes XML document segments. XSLT (extensible Stylesheet Language Transformations, Extensible Stylesheet Language Conversion) uses XPath description expressions and Address path control node selection. XSLT can transform XML into various formats, such as HTML or other formats.
The following is a mail merge program that briefly describes the XPath string functions. The following XML file contains data, and the XSLT file contains a definition of the message format. MSXML4.0 applies a style sheet to an XML document, resulting in a merged message text document.
XML file Letter.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<Letter>
<date>july, 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 style sheet Document 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= "" "/>
<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>
The above stylesheet illustrates the concat and Starts-with XPath string functions and how to add new rows to the output text, as well as defining and using variables.
The following is the result of the program's execution.
1.VC6 establishes the WIN32 console application.
2. Add the following code to the stdafx.h:
#include <tchar. H>
#include <stdio.h>
#include <time.h>
#import "Msxml4.dll"
If This import statement fails, your 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, your need to install MSXML 4.0 the SDK from:
Http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/001/766/msdncompositedoc.xml
You are 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. The code above introduces the MSXML4 type library, contains MSXML header files, examines HRESULT values, and declares some global variables.
4.main function:
int main (int argc, char* argv[])
{
Try
{
Eval_hr (CoInitialize (NULL));
Make sure this MSXML 4.0 is installed
if (!ismsxmlinstalled ())
return-1;
//Make sure this XML and XSL file names are passed
//As command line parameters
if (ARGC & Lt 3)
//Show proper message here
return-1
Ixmldomdocument2ptr pxmldoc = NULL;
Ixmldomdocument2ptr pxsldoc = NULL;
//Load the XML document
if (loaddocument (Pxmldoc, argv[1), true)
{
& nbsp 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 files and XSLT style sheet file names are passed to the application as command-line arguments. The main function verifies that the MSXML4.0 is installed by calling Ismsxmlinstalled. The next two calls Loaddocument, loading the XML document first, and then loading the XSLT style sheet. Finally, the transformnode is invoked to convert.
6. This example code download: Http://www.perfectxml.com/CPPMSXML/downloads/20020716MailMerge.zip
Translated from Perfectxml