您好,登錄后才能下訂單哦!
小編給大家分享一下XPath串函數(shù)和XSLT的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
XPath(xml Path language)是一種處理XML文檔段的語(yǔ)言。XSLT(Extensible Stylesheet Language Transformations,可擴(kuò)展樣式表語(yǔ)言轉(zhuǎn)換)使用XPath描述表達(dá)式和地址路徑控制節(jié)點(diǎn)選取。XSLT可以將XML轉(zhuǎn)換為各種格式,如HTML或其他格式。
下面用一個(gè)郵件合并程序來(lái)簡(jiǎn)要說(shuō)明XPath的串函數(shù)。下面的XML文件中包含數(shù)據(jù),XSLT文件中包含對(duì)郵件格式的定義。MSXML4.0對(duì)XML文檔應(yīng)用樣式表,產(chǎn)生一個(gè)合并的郵件文本文檔。
XML文件 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樣式表文檔 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>
上面的樣式表舉例說(shuō)明了concat和starts-with XPath串函數(shù)和怎樣在輸出文本中增加新行,還有定義和使用變量。
下面是程序的執(zhí)行結(jié)果。
1.VC6建立Win32控制臺(tái)應(yīng)用程序。
2.在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.上面的代碼引入MSXML4類(lèi)型庫(kù),包含MSXML頭文件,檢查HRESULT值并聲明了一些全局變量。
4.main函數(shù):
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文件和XSLT樣式表文件名作為命令行參數(shù)傳遞給應(yīng)用程序。主函數(shù)通過(guò)調(diào)用isMSXMLInstalled驗(yàn)證 MSXML4.0是否安裝。接下來(lái)兩次調(diào)用loadDocument;先是加載XML文檔,然后是加載XSLT樣式表。 最后調(diào)用transformNode進(jìn)行轉(zhuǎn)換。
以上是“XPath串函數(shù)和XSLT的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。