


Brief analysis of XPath string functions and XSLT with examples
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>
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 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++'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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

XPath is a very useful tool when working with XML data using PHP. XPath is a language for locating elements in XML documents. It helps developers quickly and easily extract the required data from XML documents. In this article, we will introduce the basic concepts of XPath and explain in detail how to use XPath in PHP. We will demonstrate how to use XPath to extract data from an XML document and build a simple

As Web technology continues to develop, the content of Web pages is becoming more and more complex. We often need to extract information from HTML pages for further processing and analysis, such as crawlers, data mining, etc. This article will introduce how to use PHP and XPath to parse HTML content and obtain the information we need quickly and easily. PHPSimpleHTMLDOMParserPHPSimpleHTMLDOMParser is an open source

The XSLT extension for PHP is a powerful tool for converting XML documents into HTML, XHTML or XML formats. This extension is based on the W3CXSLT standard and can quickly parse and transform XML documents. In this article, we will discuss how PHP uses XSLT extensions. 1. Install the XSLT extension. Before starting to use the XSLT extension, you need to confirm whether the extension is installed. You can use the phpinfo() function to view it. If the extension is not installed, you need to configure it in PHP

First, what is xPath: xPath is a language for finding information in xml. In xPath, there are seven elements of nodes: elements, attributes, text, namespaces, processing instructions, comments, and documents (root nodes). XML documents are parsed as document trees, and the root of the tree is called the document node or root node. This is the source code of a basic XML document. As can be seen from this XML source code, bookstore is the document node (root node), and book, title, author, year, and price are element nodes. The book node has four child element nodes: title, author, year, price, and the title node has three siblings: au

Overview of how to use PHP extension XSLT to convert XML and XHTML: In web development, it is often necessary to convert XML files to XHTML (or HTML) format for display on the browser. PHP provides XSLT extensions to make this conversion process simple and efficient. This article will introduce how to use PHP to extend XSLT to achieve XML to XHTML conversion and provide code examples. Step 1: Install and enable the XSLT extension Before starting, make sure your PHP environment is

XPath is a language for querying and locating specific nodes in XML and HTML documents. As a path expression language, XPath is widely used in many programming languages, including PHP. In this article, we will take an in-depth look at the use of PHPXPath functions so that you can easily use XPath in your projects to search and query XML and HTML files. What is XPath? XPath is a language for querying and locating specific nodes in XML and HTML documents.

1.xpath1.1xpath Use Google to install the xpath plug-in in advance. Press ctrl+shift+x and a small black box will appear to install the lxml library pipinstalllxml-ihttps://pypi.douban.com/simple. Import lxml.etreefromlxmlimportetreeetree.parse() to parse the local file html_tree =etree.parse('XX.html')etree.HTML() server response file html_tree=etree.HTML(respon

In recent years, with the continuous development of the Internet, web development technology has also been continuously updated and iterated. Among them, PHP language is widely used in the field of Web development because of its easy to learn and use, fast running speed, and cross-platform characteristics. In PHP, DOM and XPath technologies are commonly used technologies when developing Web applications. This article will introduce the basic knowledge and application scenarios of these two technologies in detail. 1. DOM technology DOM (Document Object Model, DocumentObjectModel) is a way to process XML or HTM
