XML/XSLT FAQ

Table of contents

1. Why can't I use processing-instruction() in XPath expressions to select the XML declaration (<?xml ... ?>)?
2. I am using MSXML's DOM / Microsoft's XML Notepad to add nod es to a document. Somebody is inserting xmlns="" on child elements. What's going on?
3. Where can I find an up-to-date list of XSLT engines?
4. Why did my XSLT transformation stop to work after I added a namespace declaration to the source document?
5. Why does the encoding information disappear from the XML declaration when I use the ".xml" property in MSXML's DOM?
6. Can I contact you directly for XML/XSLT contracting work?
7. I have another question not listed here.

Questions and answers

1. Why can't I use processing-instruction() in XPath expressions to select the XML declaration (<?xml ... ?>)?

The XML declaration takes a form similar to a processing instruction, but it isn't one (see section 2.6 in the XML recommendation for details). There is no portable way to get information from the XML declaration - it's not part of the information model defined for XML documents processed by XSLT.

2. I am using MSXML's DOM / Microsoft's XML Notepad to add nodes to a document. Somebody is inserting xmlns="" on child elements. What's going on?

This happens when the document you are working is using namespaces, but new nodes are being created using the non namespace-aware DOM level 1 functions. In this case DOM assumes they are in the "empty" namespace, and correctly inserts this namespace declararation. Use createNode instead, specifying the proper namespaceURI for the new element (it knows about namespace handling). If you're using XML Notepad, you'll have to wait for Microsoft to upgrade it to become namespace-aware.

3. Where can I find an up-to-date list of XSLT engines?

Check out the XSLT section on xmlsoftware.com.

4. Why did my XSLT transformation stop to work after I added a namespace declaration to the source document?

The XPath expressions in an XSLT transformation are namespace aware. Unqualified (unprefixed) element names in XPath expressions only match elements in the empty namespace. To match elements from other namespaces, the namespace needs to be declared within the XSLT file (with a prefix). You can then use this prefix within XPath expressions.

For instance, let's suppose that you have added a namespace declaration to your top level element <order>:
<order xmlns='x-schema:order.xdr'>
<id>123</id>
<item>XSLT Programmer's Reference</item>
</order>

First add the namespace declaration with a prefix to your stylesheet, for instance:
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:myns='x-schema:order.xdr' version='1.0'>
<xsl:template match='/'> ...pattern for document element... </xsl:template>
</xsl:stylesheet>

Now the newly defined prefix can be used for matching element names:
<xsl:template match='myns:order'> ...pattern for order element... </xsl:template>

Note that the matching depends on the namespace name. The XSLT's author does not need to know whether or which a prefix was actually used in the source document!

5. Why does the encoding information disappear from the XML declaration when I use the ".xml" property in MSXML's DOM?

In general, the original character encoding of the document is irrelevant once it is loaded into the DOM. Within the DOM, the character encoding is always UTF-16, as this is the native string encoding for COM objects on the Microsoft platform.

When taking the ".xml" property of a node in the DOM, the object model is serialized into a COM-conformant string (BSTR), which - by definition - is encoded in UTF-16. This is why the encoding information will never appear here.

However, when loading a DOM using the "load()" method, MSXML preserves the name of the original encoding by storing it in a processing instruction node (which by the way is a conformance bug, because the XML declaration is not a processing instruction - see section 2.6 in the XML recommendation for details). When serializing the DOM using "save()", MSXML will take this value into account, so that by using "load()" and "save()", the original encoding will be preserved. As long as the "PI hack" is present in MSXML, this can also be used to programmatically change the encoding before calling "save()" (by manipulating the appropriate processing instruction node).

6. Can I contact you directly for XML/XSLT contracting work?

Sure, just email us to learn about availability and pricing.

7. I have another question not listed here.

There are lots of good resources about XML and related topics. Here are a few of them:

Books Building Oracle XML Applications (O'Reilly, ISBN 1565926919): lots of information about combining XML and Oracle.
XSLT Programmer's Reference 2nd edition (Wrox, ISBN 1861003129): by Michael Kay, author of Saxon.
XSLT and XPath On The Edge, Unlimited Edition (Mandtbooks, ISBN 0764547763): by Jeni Tennison.
FAQs XSL Frequently Asked Questions, maintained by Dave Pawson
Jeni's XSLT pages
Microsoft XML FAQ
Unofficial MSXML XSLT FAQ, maintained by Joshua Allan
Mailing lists XSL List
News XML Hack (XML developer news)
Newsgroups microsoft.public.dotnet.xml - Microsoft's newsgroup for XML in the .NET environment
microsoft.public.xml - Microsoft's generic XML newsgroup
microsoft.public.xml.msxml-webrelease - Microsoft's generic newsgroup for the current preview of MSXML
microsoft.public.xsl - Microsoft's generic XSL newsgroup
Specs W3C home page


Our partners

XML Spy