Author: Emmanuel KARTMANN <emmanuel@kartmann.org>.
Date: January 26th, 2000
![]()
![]()
a programming language for transforming XML documents: XSL Transformations (XSLT)
and XML Path Language (XPath). This language provides pattern matching
(xsl:template match),
conditional statements (xsl:when test), loops (for-each),
etc...
| an XML vocabulary for specifying formatting semantics: similar to W3C cascading style sheets (CSS),
this vocabulary provides enhanced presentation features.
| |
Note that XSL is expressed in XML.
Here's a simple XSL file which transforms the XML file above (the mailbox model) into an HTML table:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<TABLE> <- start the table
<TR> <- first row: header (From/To/Subject)
<TD>From</TD>
<TD>To</TD>
<TD>Subject</TD>
</TR>
<xsl:for-each select="MAILBOX/MESSAGES/MESSAGE"> <- loop on every message
<TR> <- start row (one row per message)
<TD><xsl:value-of select="HEADER/FROM"/></TD> <- print the value of FROM
<TD><xsl:value-of select="HEADER/TO"/></TD> <- print the value of TO
<TD><xsl:value-of select="HEADER/SUBJECT"/></TD> <- print the value of SUBJECT
</TR> <- end row
</xsl:for-each> <- next message
</TABLE>
</xsl:template>
</xsl:stylesheet>
Using XML in combination with XSL, you can truly separate data from presentation (and make them evolve separately). For example, the same XML data could be transformed into another HTML table with a completely different style. Please refer to the sample code (JavaScript) to see an example (plain and fancy tables generated from the same XML file).
Load an XML document, i.e. create a tree structure in memory based on an XML file (or buffer).
The XMLDOM can even download an XML document from a remote location (using an URL as the document
location). See methods
IXMLDOMDocument::load and
IXMLDOMDocument::loadXML
for details.
| Save an XML document, i.e. create an XML file based on the tree structure in memory representing the
XML document. See method
IXMLDOMDocument::save
for details.
| Access the document tree, i.e. walk through the tree, select one or several nodes, etc... Microsoft provides
several COM objects (interfaces) to do so: XMLDOMNode,
XMLDOMNodeList,
IXMLDOMNamedNodeMap.
| Add or remove nodes in the document tree. See methods IXMLDOMDocument::createNode,
IXMLDOMDocument::createTextNode,
IXMLDOMDocument::createElement,
IXMLDOMNode::appendChild,
and IXMLDOMNode::removeChild
for details.
| Parse an XML document and transform it through an XSL stylesheet. See methods IXMLDOMNode::transformNode
and IXMLDOMNode::transformNodeToObject
for details.
| |
The XMLDOM handles document tree as follows:
Every node has a name (a string). Optionally, the name can be empty.
| Every node has a type: NODE_ELEMENT (for tags), NODE_ATTRIBUTE (for attributes), NODE_TEXT (for text within a tag), etc... | Please refer to the DOMNodeType enumeration for details. Every element node (tags) can have one or more attributes (e.g. "STYLE" of the "TR" node). Attributes are stored in children
nodes of type NODE_ATTRIBUTE.
| Every text node has one property: the text (e.g. "emmanuel@kartmann.org" in our example).
| |
XML Text String mapped to XML Document Tree: Tags with attributes
XML Text String mapped to XML Document Tree: Tags with text
XMLBuildTree: creates an XML document tree in memory and prints it in stdout.
| XMLTransform: transforms an XML file using an XSL stylesheet.
| |
// =======================================================================
// Include XMLDOM definition
// =======================================================================
#include <msxml.h>
// =======================================================================
// Initialize COM (CAUTION: ALL THREADS MUST CALL THIS FUNCTION)
// =======================================================================
HRESULT hResult = CoInitialize(NULL);
if (FAILED(hResult)) {
cerr << "Cannot initialize COM: Error "
<< GetLastError()
<< endl;
} else {
// =======================================================================
// Declare XMLDOM variables
// =======================================================================
IXMLDOMDocument *pXMLDocument = NULL;
IXMLDOMDocument *pXMLStyle = NULL;
// =======================================================================
// Get the CLSID of the "Microsoft.XMLDOM" object
// (this is a trick to check that the component is installed)
// =======================================================================
LPCOLESTR lpszProgID = L"Microsoft.XMLDOM";
CLSID clsid;
hResult = CLSIDFromProgID(lpszProgID, &clsid);
if (FAILED(hResult)) {
cerr << "Microsoft XMLDOM is not installed on your system!" << endl;
} else {
// =======================================================================
// Create first XML Document Object Model (XMLDOM)
// =======================================================================
hResult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXMLDocument);
if (FAILED(hResult)) {
cerr << "Cannot create 1st Microsoft XMLDOM: Error "
<< GetLastError()
<< endl;
} else {
// =======================================================================
// Create second XML Document Object Model (XMLDOM)
// =======================================================================
hResult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXMLStyle);
if (FAILED(hResult)) {
cerr << "Cannot create 2nd Microsoft XMLDOM: Error "
<< GetLastError()
<< endl;
} else {
_bstr_t BXMLDocumentFile = "MyDocument.xml";
_bstr_t BXMLStyleFile = "MyStyle.xml";
int nIndentStep = 4; // 4 spaces
VARIANT_BOOL vb;
// =======================================================================
// Load XML Document (synchrone)
// =======================================================================
pXMLDocument->put_async(VARIANT_FALSE);
hResult = pXMLDocument->load(_variant_t(BXMLDocumentFile), &vb);
if (FAILED(hResult)) {
cerr << "Cannot load XML document"
<< (LPCTSTR)BXMLDocumentFile
<< endl;
} else {
// =======================================================================
// Load XML Style (synchrone)
// =======================================================================
pXMLStyle->put_async(VARIANT_FALSE);
hResult = pXMLStyle->load(_variant_t(BXMLStyleFile), &vb);
if (FAILED(hResult)) {
cerr << "Cannot load XML document"
<< (LPCTSTR)BXMLStyleFile
<< endl;
} else {
CString szXMLString;
// =======================================================================
// Transform XML with XSL
// =======================================================================
BSTR pBXMLString = NULL;
VARIANT vObject;
IXMLDOMDocument *pOutputXMLDocument = NULL;
IDispatch *pDisp = NULL;
hResult = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pOutputXMLDocument);
hResult = pOutputXMLDocument->QueryInterface(IID_IDispatch, (void **)&pDisp);
vObject.vt = VT_DISPATCH;
vObject.pdispVal = pDisp;
// Use transformNodeToObject
hResult = pXMLDocument->transformNodeToObject(pXMLStyle, vObject);
if (!FAILED(hResult)) {
CXMLTool::MakeXMLBuffer(pOutputXMLDocument, szXMLString, nIndentStep);
}
// =======================================================================
// Print the transformed string
// =======================================================================
cout << (LPCTSTR)szXMLString;
}
}
}
}
}
}
// =======================================================================
// UnInitialize COM
// =======================================================================
CoUninitialize();
The CXMLTool used in this example is available in the subdirectory SampleCodeVCPP. For more about the CXMLTool class, please refer to the CXMLTool HTML documentation.
| Extensible Markup Language (XML) at W3C Web Site http://www.w3.org/XML/
XML in 10 points (explains XML briefly) | http://www.w3.org/XML/1999/XML-in-10-points
Extensible Markup Language (XML) 1.0, W3C Recommendation 10-February-1998 | http://www.w3.org/TR/REC-xml
Extensible Stylesheet Language (XSL) Version 1.0, W3C Working Draft 12 January 2000 | http://www.w3.org/TR/xsl/
XSL Transformations (XSLT) Version 1.0, W3C Recommendation 16 November 1999 | http://www.w3.org/TR/xslt
XML Path Language (XPath) Version 1.0, W3C Recommendation 16 November 1999 | http://www.w3.org/TR/xpath
Document Object Model (DOM) at W3C Web Site | http://www.w3.org/DOM/
Namespaces in XML, World Wide Web Consortium 14-January-1999 | http://www.w3.org/TR/REC-xml-names/ Microsoft
Extensible Markup Language (XML) at Microsoft's Web Site | http://msdn.microsoft.com/xml/default.asp
"Introduction to XML", MSDN Online Documentation | http://msdn.microsoft.com/xml/general/intro.asp
"Why XML?", MSDN Online Documentation | http://msdn.microsoft.com/xml/general/whyxml.asp
"XML: The ASCII of the Future?", Steve Land, Corbis | http://msdn.microsoft.com/library/techart/xmlfinal.htm
"XML Developer's Guide", MSDN Online Documentation | http://msdn.microsoft.com/library/psdk/xmlsdk/xmlp91b9.htm
"A Beginner's Guide to the XML DOM", Brian Randell, DevelopMentor | http://msdn.microsoft.com/xml/articles/beginner.asp
"Using the XMLDOMDocument Object", MSDN Online Documentation | http://msdn.microsoft.com/xml/xmlguide/dom-guide-document.asp
"Getting started with XSL", MSDN Online Documentation | http://msdn.microsoft.com/xml/xslguide/xsl-overview.asp
"Getting Started with XSL", MSDN Online Documentation | http://msdn.microsoft.com/xml/xslguide/xsl-overview.asp
"Using the XSL Processor", MSDN Online Documentation | http://msdn.microsoft.com/xml/xslguide/transform-overview.asp
Downloading XML Notepad | http://msdn.microsoft.com/xml/notepad/download.asp
Microsoft XML Parser Redistributable (old) | http://msdn.microsoft.com/downloads/tools/xmlparser/xmlparser.asp
Microsoft XML Parser Technology Preview Release (new) | http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp
Microsoft Windows DNA XML Resource Kit | http://msdn.microsoft.com/vstudio/xml/default.asp
DNA XML Resource Kit: Location: Development Platform, English Pack, Disc 1, December 1999 | http://msdn.microsoft.com/subscriptions/resources/subdwnld.asp
Microsoft Windows DNA XML Resource Kit Updates, MDN Online | http://msdn.microsoft.com/workshop/xml/general/DnaXmlRsk.asp
XML and XSL Demos, MSDN Online | http://msdn.microsoft.com/workshop/xml/general/xmlxsldemo.asp Microsoft Newsgroup on XML news://microsoft.public.xml Others
XML.ORG, The XML Industry Portal, a cool site referencing XML-related subjects and software | http://www.xml.org/
OASIS, Organization for the Advancement of Structured Information Standards | http://www.oasis-open.org/
"XML in an Instant: A Non-geeky Introduction" by Charles Goldfarb (inventor of SGML), OASIS White Paper | http://www.xmlbooks.com/press/nongeeky.htm
"XML, Java, and the future of the Web", by Jon Bosak (Sun Microsystems) | http://metalab.unc.edu/pub/sun-info/standards/xml/why/xmlapps.htm
"XML - Questions & Answers", by Jon Bosak (Sun Microsystems) | http://www.isgmlug.org/n3-1/n3-1-18.htm
"An Introduction to XML Processing with Lark and Larval", by Tim Bray (XML Parsers in Java) | http://www.textuality.com/Lark/
W3Schools.com (XML Tutorial) | http://www.w3schools.com/xml/default.asp
XML 101 | http://www.xml101.com
CommerceNet's XML Exchange | http://www.xmlx.com
Java Technology and XML | http://java.sun.com/xml/
Java Project X Core Library | http://java.sun.com/xml/docs/api/overview-summary.html
Java Package com.sun.xml.parser, | http://java.sun.com/xml/jaxp/dist/1.0.1/docs/api/internal/com/sun/xml/parser/package-summary.html
The <?XML!> FAQ | http://www.ucc.ie/xml
<?XML?>.com (O'Reilly & Associates) | http://www.xml.com/xml/pub |