Поделиться через


Frequently Asked Questions about XSLT

 

The following are frequently asked questions about XSLT.

How do I run XSLT transformations programmatically?

XSLT transformations can be initiated programmatically using any language that supports COM interfaces. Such programming languages include Microsoft JScript, VBScript, Visual Basic, C++, and even Perl. To run XSLT transformations programmatically, you must create two XML DOM objects, one for the XML source document and the other for the XSLT style sheet. Then, you call the transformNode() function on the XML source document with the XSLT style sheet XML DOM object as the argument. The following example in JScript illustrates these points.

JScript File (test.js)

var xmlstr = "<object><name>apple</name><color>Red</color></object>";  
var xsltstr = '<?xml version="1.0"?>' +   
      '<xsl:stylesheet ' +  
      '      version="1.0" ' +  
      '      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> '+  
      '   <xsl:output method="text"/> '+  
      '   <xsl:template match="/object"> '+  
      '      <xsl:value-of select="color"/> '+  
      '      <xsl:text> </xsl:text> '+  
      '      <xsl:value-of select="name"/> '+  
      '   </xsl:template> '+  
      '</xsl:stylesheet>';  
  
var xmldom, xsltdom;  
try {  
   xmldom  = new ActiveXObject("Msxml2.DOMDocument.6.0");  
   xmldom.validateOnParse = true;  
   xmldom.async = false;  
   xmldom.loadXML(xmlstr);  
  
   xsltdom = new ActiveXObject("Msxml2.DOMDocument.6.0");  
   xsltdom.validateOnParse = true;  
   xsltdom.async = false;  
   xsltdom.loadXML(xsltstr);   
  
   output = xmldom.transformNode(xsltdom);  
   WScript.echo(output);  
}  
catch(err) {  
   WScript.echo(err.description);  
}
Try It!
  1. Copy and paste the code into a file, and save it as test.js.

  2. Type the "test.js" command from a command window.

Output

The output is "Red apple".

Do I need to use a different XSLT namespace with Internet Explorer?

No. Use the standard xmlns:xsl="http://www.w3.org/1999/XSL/Transform" syntax. If Internet Explorer returns an error when you use this namespace, it is likely that you are using an earlier version of MSXML that does not support XSLT. If you have older XSL files and do not want to convert them to XSLT files, you can still use the namespace declaration xmlns:xsl="http://www.w3.org/TR/WD-xsl".

Does MSXML provide a 100% compliant XSLT processor?

Yes. MSXML is fully compliant with the XSLT specification. For more information, see Supported XSLT Features.

Why was the MSDN online documentation for XSL so different from the XSLT recommendation?

Because XSLT is a different technology from XSL.

Where can I find documentation about MSXML compliance?

Each new release of MSXML has a Bug List Page that describes known problems, such as coding mistakes or features that are not fully implemented. To find this page, search MSDN. For full conformance disclosure, see Supported XSLT Features.

If you find a bug or implementation point that is not clearly documented, please send feedback to the XML documentation team by using the XML Documentation Feedback form. To use this form, click the Feedback icon (the envelope) at the top-right corner of any page of this documentation.

How can I call MSXML from the command prompt to do batch processing of XSLT?

Because MSXML is a COM object, you can write VBScript, JScript, or other Windows Script Host (WSH) files to launch MSXML from the command prompt.

What can I do if I wrote lots of XSL using the old Internet Explorer version of XSL?

If you would like to upgrade XSL files so that they are compliant with the XSLT recommendation, you can use the XSL to XSLT 1.1 Converter available from MSDN Downloads at https://msdn.microsoft.com/downloads/.

I updated a style sheet I wrote previously to transform ADO-persisted XML recordsets. I now get an "undeclared reference to namespace prefix" error. What do I need to do to fix this?

The reason for this error involves namespaces that ActiveX Data Objects (ADO) 2.6 uses when you use the adPersistXML formatting option with the Save method to persist an ADO recordset as XML.

For example, the following line shows the syntax used to persist the current ADO recordset as XML in a Visual Basic application.

rs.Save "c:\temp\nwind.xml ", adPersistXML

The persisted XML output includes several namespace prefixes: "s", "dt", "rs", and "z". When you use XSLT style sheets to transform ADO-persisted XML, these namespaces must be declared for the transformation to succeed.

When using ADO to persist data as XML, you might encounter the following error text when you attempt to apply XSLT to your persisted XML.

'undeclared reference to namespace prefix'  

For example, a style sheet might reference the XSLT namespace URI as follows:

<xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="catalog">
            <xsl:value-of select="//z:row/@isbnnumber"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This example, though valid as XSLT, produces the undeclared namespace error described above. To fix the problem, style sheets that specify the XSLT namespace URI must also declare any of the ADO namespaces that are used as input.

The following example shows how to update the previous style sheet code to declare these namespaces.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
        xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
        xmlns:rs='urn:schemas-microsoft-com:rowset'
        xmlns:z='#RowsetSchema' version='1.0'>
    <xsl:template match="/">
        <xsl:element name="catalog">
            <xsl:value-of select="//z:row/@isbnnumber"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

By adding the ADO namespace declarations above to your style sheet, you can correct the error and permit it to successfully transform ADO-persisted XML.

Style sheets that reference the older XSL namespace (like the following sample) do not produce this error.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
        <xsl:element name="catalog">
            <xsl:value-of select="//z:row/@isbnnumber"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Therefore, these style sheets are not affected and do not require any changes for this issue.

Will MSXML support XSLT 2.0?

No. MSXML versions 6.0 fully implement and support XSL Transformations (XSLT) Version 1.0 (W3C Recommendation 16 November 1999).