Sady Home

Note my coding life

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
To continue XPath:
Selecting Nodes (VBScript)
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("books.xml")
xmlDoc.selectNodes(path expression)

For example: xmlDoc.selectNodes("/bookstore/book[0]/text()")

What is XSLT?

  • XSLT stands for XSL Transformations
  • XSLT is the most important part of XSL
  • XSLT transforms an XML document into another XML document
  • XSLT uses XPath to navigate in XML documents
  • XSLT is a W3C Recommendation
XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. XSLT uses XPath to find information in an XML document.
 
How to transform XML into XHTML using XSLT
Start with a Raw XML Document
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  
<cd>
    
<title>Empire Burlesque</title>
    
<artist>Bob Dylan</artist>
    
<country>USA</country>
    
<company>Columbia</company>
    
<price>10.90</price>
    
<year>1985</year>
  
</cd>

</catalog>

Create an XSL Style Sheet (such as cdcatalog.xsl)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  
<html>
  
<body>
    
<h2>My CD Collection</h2>
    
<xsl:apply-templates/>
    
<table border="1">
    
<tr bgcolor="#9acd32">
      
<th align="left">Title</th><th align="left">Artist</th>
    
</tr>
    
<xsl:for-each select="catalog/cd[artist!='Bob Dylan']">
    
<xsl:sort select="artist"/>
    
<xsl:if test="price &gt; 10">
    
<tr>
      
<td><xsl:value-of select="title"/></td>
      
<xsl:choose>
        
<xsl:when test="price &gt; 10">
          
<td bgcolor="#ff00ff"><xsl:value-of select="artist"/></td>
        
</xsl:when>
        
<xsl:otherwise>
          
<td><xsl:value-of select="artist"/></td>
        
</xsl:otherwise>
      
</xsl:choose>
    
</tr>
    
</xsl:if>
    
</xsl:for-each>
    
</table>
  
</body>
  
</html>
</xsl:template>
</xsl:stylesheet>

Transforming XML to XHTML in the Browser
<script type="text/javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async 
= false
xml.load(
"cdcatalog.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async 
= false
xsl.load(
"cdcatalog.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

Transforming XML to XHTML on the Server (ASP)
<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async 
= false
xml.load(Server.MapPath(
"cdcatalog.xml"))

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async 
= false
xsl.load(Server.MapPath(
"cdcatalog.xsl"))

'Transform file
Response.Write(xml.transformNode(xsl))
%
>

Open, Edit and Save XML...

* Reference to W3C Schools
posted on 2007-10-27 01:55  Sady  阅读(287)  评论(0)    收藏  举报
凭飞堂