Search code examples
xmldelphixsltmsxml

What can be done when XSLT transformation using msxml doesn't match root node ('/')?


I'm running an XSLT 1.0 transformation using Delphi/MSXML. The XSLT goes like

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:lh="http://localhost" 
                version="1.0">

    <xsl:output method="html" omit-xml-declaration="yes" indent="yes" encoding="ISO-8859-1" />

    <xsl:template match="@*|node()">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="/">
        <html>
            <body>
                <h2>My Book Collection</h2>
                <table border="1">
                    <tr>
                        <th>Author</th>
                        <th>Title</th>
                    </tr>
                    <xsl:for-each select="lh:library/lh:book">
                        <tr>
                            <td><xsl:value-of select="@author"/></td>
                            <td><xsl:value-of select="."/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

and the XML books.xml is define as

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://localhost">
    <book author="Michael Howard">Writing Secure Code</book>
    <book author="Michael Kay">XSLT Reference</book>
</library>

When I run this XSLT transformation using Delphi/MSXML, it doesn't output anything.

Saxon, for reference, yields the following result (1.0/2.0 warning message not included):

<html xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:lh="http://localhost">
   <body>
      <h2>My Book Collection</h2>
      <table border="1">
         <tr>
            <th>Author</th>
            <th>Title</th>
         </tr>
         <tr>
            <td>Michael Howard</td>
            <td>Writing Secure Code</td>
         </tr>
         <tr>
            <td>Michael Kay</td>
            <td>XSLT Reference</td>
         </tr>
      </table>
   </body>
</html>

What can I do about the XML (the XSLT is not really for me to change) to give the same output as in Saxon?


Solution

  • Try to run the transformation with the document nodes; change in the original code this line:

    XML.DocumentElement.TransformNode(XSL.DocumentElement, Result)
    

    to:

    XML.Node.TransformNode(XSL.Node, Result)
    

    If you explictly call the transformNode on the documentElement node then it does not surprise me that the match="/" does not get applied.