© 2004 Microsoft Corporation. All rights reserved.
Figure 1 Typical XML Document
<!DOCTYPE people [
  <!ELEMENT people (person*)>
  <!ELEMENT person (name, spouse)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT spouse EMPTY>
  <!ATTLIST person id ID #REQUIRED>
  <!ATTLIST spouse href IDREF #IMPLIED>
]>
<people>
  <person id='ss-555-12-4343'>
    <name>Aaron</name>
    <spouse href='ss-555-88-7474'/>
  </person>
  <person id='ss-555-88-7474'>
    <name>Monica</name>
    <spouse href='ss-555-12-4343'/>
  </person>
</people>
Figure 2 XML Schema
<soap:Envelope xmlns:x='urn:some:ns' 
   xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
   soap:encodingStyle=
      'http://schemas.xmlsoap.org/soap/encoding'
   >
  <soap:Body>	
    <x:Compare>
      <p1 href='#aaron'/>
      <p2 href="#aaron'/>
    </x:Compare>
    <x:Person id='aaron'>
      <name>Aaron Skonnard</name>
    </x:Person>
  </soap:Body>
</soap:Envelope>
Figure 3 invoice.xsd
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
            xmlns:tns='urn:www-develop-com:invoices'
            targetNamespace='urn:www-develop-com:invoices'
            xmlns='urn:www-develop-com:invoices' 
>

  <xsd:complexType name='LineItem'>
    <xsd:sequence>
      <xsd:element name='Sku' type='skuType'/>
      <xsd:element name='Description' type='xsd:string'/>
      <xsd:element name='Price' type='xsd:double'/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name='LineItems'>
    <xsd:sequence>
      <xsd:element name='LineItem' type='tns:LineItem' minOccurs='1' 
          maxOccurs='unbounded'  />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name='Invoice'>
    <xsd:sequence>
      <xsd:element name='InvoiceID' type='xsd:string'/>
      <xsd:element name='CustomerName' type='xsd:string'/>
      <xsd:element name='LineItems' type='tns:LineItems'/>
    </xsd:sequence>
  </xsd:complexType>  

  <xsd:element name='Invoice' type='Invoice'/>

  <xsd:simpleType name='skuType'>
    <xsd:restriction base='xsd:integer' >
      <xsd:minInclusive value='100' />
      <xsd:maxInclusive value='999' />
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>
Figure 4 Namespace Node Properties

Property Name
Description
local-name
Namespace prefix
namespace-uri
"" (empty string)
string-value (as if by calling the string function)
Namespace URI
parent::node
The element node to which the namespace node belongs
Figure 6 XPath Date/Time Extension Functions

Function Name
Description
ms:utc
Converts XSD date/time representations to the UTC representation: YYYY-MM-DDTHH:NN:SS.III (where III represents milliseconds)
ms:format-date
Converts XSD date representations to the specified format, acceptable for output
ms:format-time
Converts XSD time representations to the specified format, acceptable for output
Figure 7 XInclude
Dim f As New VBSAXXIncludeFilter
Dim filter As IVBSAXXMLFilter
Dim r As New SAXXMLReader30
Dim reader As IVBSAXXMLReader
Dim ser As New Serializer

r.putFeature _ 
"http://xml.org/sax/features/namespace-prefixes", False
Set filter = f
Set filter.Parent = r
Set reader = filter    
Set reader.contentHandler = ser   
reader.parseURL "person.xml"
Figure 8 Just XML Inclusion
<xsl:transform
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xinc="http://www.w3.org/2001/XInclude"
  version="1.0"
  >
  <xsl:template match="xinc:include">
    <xsl:apply-templates select="document(@href)"/>
  </xsl:template>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>
Figure 9 menu.xml
<?xml-stylesheet type="text/xsl" href="menu.xsl"?>
<menusystem>  
  <menu>
    <item label="Home" href="home.asp" />
    <item label="Login" href="login.asp" />
    <item label="About Us">
        <item label="Who we are" href="us.asp" />
        <item label="Where we are" href="where.asp" />
        <item label="Contact Information" href="contact.asp" />
    </item>
    <item label="Products">
        <item label="Product1" href="product1.asp" />
        <item label="Product2" href="product2.asp" />
        <item label="Product3" href="product3.asp" />
        <item label="Product4" href="product4.asp" />
    </item>
    <item label="Specials" href="specials.asp" />
  </menu>
</menusystem>
Figure 10 menu.xsl Transform
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" version='1.0'>
    <xsl:script>
       var currID;
       function    getID(node)    {
          currID = "ID"    + uniqueID(node);
          return currID;
       }
    </xsl:script>
    <xsl:template match="/menusystem/menu/item">
        <div>
            <li>
                <xsl:attribute name="onmouseout">normal(this)
                </xsl:attribute>
                <xsl:choose>
                    <xsl:when test=".[not(item)]">
                        <xsl:attribute name="onclick">
                        menuClick(<xsl:eval>getID(this)</xsl:eval>,
                        '<xsl:value-of select="@href" /> 
                         event.cancelBubble=true;</xsl:attribute>
                        <xsl:attribute name="class">mainitemleaf
                        </xsl:attribute>
                        <xsl:attribute name="onmouseover">hiliteLeaf(this)
                        </xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:attribute name="onclick">menuClick
                        (<xsl:eval>getID(this)</xsl:eval>, ' 
                        event.cancelBubble=true;</xsl:attribute>
                        <xsl:attribute name="class">mainitemplus
                        </xsl:attribute>
                        <xsl:attribute name="onmouseover">
                        hiliteMenu(this)</xsl:attribute>
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:value-of select="@label" />
            </li>
        </div>
        <div style='display:none'>
            <xsl:attribute name="ID">
                <xsl:eval>currID</xsl:eval>
            </xsl:attribute>
            <xsl:apply-templates select="item" />
        </div>
    </xsl:template>
    <xsl:template match="/menusystem/menu/item/item">
        <div>
            <xsl:choose>
                <xsl:when test=".[not(item)]">
                    <xsl:attribute name="onclick">menuClick
                    (<xsl:eval>getID(this)</xsl:eval>, 
                    '<xsl:value-of select="@href" /> 
                    event.cancelBubble=true;</xsl:attribute>
                    <xsl:attribute name="onmouseover">hiliteLeaf(this)
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="onclick">menuClick
                    (<xsl:eval>getID(this)</xsl:eval>, ' 
                    event.cancelBubble=true;</xsl:attribute>
                    <xsl:attribute name="onmouseover">hiliteMenu(this)
                    </xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:attribute name="class">subitem1</xsl:attribute>
            <xsl:attribute name="onmouseout">normal(this)</xsl:attribute>
            <xsl:value-of select="@label" />
        </div>
        <div style='display:none'>
            <xsl:attribute name="ID">
                <xsl:eval>currID</xsl:eval>
            </xsl:attribute>
            <xsl:apply-templates select="item" />
        </div>
    </xsl:template>
    <!-- root template,    main entry point -->
    <xsl:template match="/">
      <html>
        <head>
          <title>XML/XSL Sample Menu</title>
          <link rel="stylesheet" type="text/css" href="global.css"/>
          <script SRC="global.js"></script>
        </head>
        <body bgcolor="black" leftmargin="5" topmargin="5">
          <div>
            <xsl:apply-templates select="/menusystem/menu/item"/>
          </div>
        </body>
      </html>
    </xsl:template>
  </xsl:stylesheet>
Figure 13 multipleout.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://mycompany.com/mynamespace"
                version="1.0">
  <msxsl:script language="JScript" implements-prefix="user">
    function outputDocument(fileName, nodeList) {
      var doc = new ActiveXObject("MSXML2.DOMDocument");
      doc.loadXML(nodeList.nextNode().xml);
      doc.save(fileName);
      return "";
    }
  </msxsl:script>
  
  <xsl:template match="/">
    <xsl:for-each select="book/chapter">
      <xsl:variable name="fileName" select="concat('chapter', position(), 
           '.xml')"/>
      <xsl:value-of select="user:outputDocument($fileName, .)"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Figure 14 Mapping of &#160;
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY sp "&#160;">
]>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <!-- root template -->
    <html>
      <!-- there will be 3 spaces before Hello -->
      <body>&sp;&#160;&sp;Hello World</body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Home
Topics
Issues
Columns
Downloads
Subscribe
Contact Us

MSDN Magazine