Home Backend Development XML/RSS Tutorial Brief analysis of XPath string functions and XSLT with examples

Brief analysis of XPath string functions and XSLT with examples

Mar 01, 2017 pm 04:53 PM
xpath xslt

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!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP in action: Extracting data from XML documents using XPath PHP in action: Extracting data from XML documents using XPath Jun 13, 2023 pm 10:03 PM

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

How to parse HTML content using PHP and XPath How to parse HTML content using PHP and XPath Jun 17, 2023 am 11:17 AM

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

How to use PHP's XSLT extension? How to use PHP's XSLT extension? May 31, 2023 pm 09:51 PM

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

What are the basic syntaxes for xPath injection? What are the basic syntaxes for xPath injection? May 26, 2023 pm 12:01 PM

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

How to use php extended XSLT for XML and XHTML conversion How to use php extended XSLT for XML and XHTML conversion Jul 28, 2023 pm 02:28 PM

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

Detailed explanation of PHP XPath function usage: XPath provides search and query functions for XML and HTML files Detailed explanation of PHP XPath function usage: XPath provides search and query functions for XML and HTML files Jun 27, 2023 pm 01:04 PM

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.

How to use Python for xpath, JsonPath, and bs4? How to use Python for xpath, JsonPath, and bs4? May 09, 2023 pm 09:04 PM

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

DOM and XPath technology in PHP DOM and XPath technology in PHP May 11, 2023 pm 04:04 PM

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

See all articles