XmlPathlanguage (XPath) is a language used to process XML document segments. XSLT (ExtensibleStylesheetLanguageTransformations, extensible style sheet language conversion) uses XPath description expressions and address paths to control node selection. XSLT can convert XML into various formats, such as HTML or other formats. Xml Path language (XPath) is a language used to process XML document segments. XSLT (Extensible Stylesheet Language Transformations, Extensible style sheet Language conversion) uses XPath description expressions and address paths to control node selection. XSLT can convert XML into various formats, such as HTML or other formats.
Next we will use a mail merge program to briefly describe the XPath string function. The following XML file contains data, and the XSLT file contains the definition of the Mail format. MSXML4.0 applies a style sheet to XML documents to generate a merged mail text document.
XML file 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 style sheet document Letter. xsl
To,
Regarding:
Dear
Mr.
Miss
,
Sincerely,
The style sheet above illustrates the concat and starts-with XPath string functions and how to add new lines to the output text, as well as defining and using variables.
The following is the execution result of the program.
1. create a Win32 console application in VC6.
2. add the following code to 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. the above code introduces the MSXML4 library, including the MSXML header file, checks the HRESULT value, and declares some global variables.
4. main function:
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. the XML file and the XSLT style table file name are passed to the application as command line parameters. The main function verifies whether MSXML4.0 is installed by calling isMSXMLInstalled. The next two calls loadDocument; first load the XML document, and then load the XSLT style sheet. Finally, transformNode is called for conversion.
6. download this example code: http://www.perfectxml.com/CPPMSXML/downloads/20020716MailMerge.zip
The above is an example of how to analyze the content of XPath string functions and XSLT. For more information, see PHP Chinese network (www.php1.cn )!