使用msxml的XSLT转换与根节点('/')不匹配时可以做什么?

时间:2014-10-17 14:15:47

标签: xml delphi xslt msxml

我使用Delphi / MSXML运行XSLT 1.0转换。 XSLT就像

<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>

,XML books.xml定义为

<?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>

当我运行此XSLT transformation using Delphi/MSXML时,它不会输出任何内容。

作为参考,Saxon产生以下结果(不包括1.0 / 2.0警告消息):

<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>

我可以做些什么(XSLT不是真的让我改变)来提供与Saxon相同的输出?

1 个答案:

答案 0 :(得分:3)

尝试使用文档节点运行转换;更改original code此行:

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

为:

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

如果您明确地致电transformNode节点上的documentElement,那么match="/"未获得应用并不会让我感到惊讶。