DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 實例簡析XPath串函數和XSLT
實例簡析XPath串函數和XSLT
編輯:XML詳解     

XPath(XML Path language)是一種處理XML文檔段的語言。XSLT(Extensible Stylesheet Language Transformations,可擴展樣式表語言轉換)使用XPath描述表達式和地址路徑控制節點選取。XSLT可以將XML轉換為各種格式,如Html或其他格式。
    下面用一個郵件合並程序來簡要說明XPath的串函數。下面的XML文件中包含數據,XSLT文件中包含對郵件格式的定義。MSXML4.0對XML文檔應用樣式表,產生一個合並的郵件文本文檔。
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="'&#xA;'" />
<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>
 
    上面的樣式表舉例說明了concat和starts-with XPath串函數和怎樣在輸出文本中增加新行,還有定義和使用變量。
    下面是程序的執行結果。
    


1.VC6建立Win32控制台應用程序。
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類型庫,包含MSXML頭文件,檢查HRESULT值並聲明了一些全局變量。
4.main函數:
  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樣式表文件名作為命令行參數傳遞給應用程序。主函數通過調用isMSXMLInstalled驗證    MSXML4.0是否安裝。接下來兩次調用loadDocument;先是加載XML文檔,然後是加載XSLT樣式表。 最後調用transformNode進行轉換。
6.本例代碼下載:http://www.perfectxml.com/CPPMSXML/downloads/20020716MailMerge.zip

譯自PerfectXML

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved