Home  >  Article  >  Backend Development  >  Brief analysis of XPath string functions and XSLT with examples

Brief analysis of XPath string functions and XSLT with examples

黄舟
黄舟Original
2017-03-01 16:53:372009browse

XPath (xml Path language) is a language for processing XML document segments. XSLT (Extensible Stylesheet Language Transformations, Extensible Stylesheet Language Transformations) uses XPath to describe expressions and address paths to control node selection. XSLT can convert XML into various formats such as HTML or other formats.
The following uses a mail merge program to briefly explain the XPath string function. The following XML file contains data, and the XSLT file contains the definition of the email format. MSXML4.0 applies style sheets to XML documents to produce a merged email 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 above style sheet illustrates the concat and starts-with Add new lines, and define and use variables.
The following is the execution result of the program.

1.VC6 creates a Win32 console application.
2. Add the following code in 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 buffer
static _TCHAR   szTemp[TEMP_SIZE];         // multipurpose buffer on stack
static DWord    dwLen;

3. The above code introduces the MSXML4 type library, contains 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.XML file and XSLT style sheet file names 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 to loadDocument are; first to load the XML document, then to load the XSLT stylesheet. Finally, call transformNode for conversion.

6. Download the code for this example: http://www.perfectxml.com/CPPMSXML/downloads/20020716MailMerge.zip

The above is a brief analysis of the XPath string function with an example and XSLT content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn