Brief analysis of XPath string functions and XSLT with examples

黄舟
Release: 2017-03-01 16:53:37
Original
2145 people have browsed it

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

<?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>
Copy after login

XSLT style sheet document Letter.xsl

<?xml version=&#39;1.0&#39; 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="&#39;&#xA;&#39;" />
<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, &#39; &#39;, //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, &#39; &#39;, //Address/State, &#39; &#39;, //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, &#39;M&#39;)">Mr. </xsl:if><xsl:if test="starts-with(//Sex, &#39;F&#39;)">Miss </xsl:if>
<xsl:value-of select="concat(//To/FirstName, &#39; &#39;, //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, &#39; &#39;, //Sender/LastName)" />
<xsl:value-of select="$NL" />
<xsl:value-of select="//Sender/Title" />
</xsl:template>
</xsl:stylesheet>
Copy after login

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 <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++&#39;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;
Copy after login

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;
}
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template