我可以向XSLT输出添加XML声明吗?

Ada*_*m L 4 xml xslt

这是我当前的XML输出:

    <EmployeeImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
    <EmployeeNumber>123</EmployeeNumber>
    <FirstName>Jose</FirstName>
    </Employee>
    </EmployeeImport>
Run Code Online (Sandbox Code Playgroud)

等等.我希望得到的输出如下:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <EmployeeImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
    <EmployeeNumber>123</EmployeeNumber>
    <FirstName>Jose</FirstName>
    </Employee>
    </EmployeeImport>
Run Code Online (Sandbox Code Playgroud)

我希望能够将第一行添加到我的输出中.在我的XSLT中,我试过了&lt;

打印"<"但它被解释为&lt;.我也尝试过<xsl:text>,但遇到了同样的问题.有没有办法将这个声明行添加到我的XSLT?

Dan*_*ley 8

只需使用xsl:output.

例...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output version="1.0" encoding="UTF-8" standalone="yes"/>

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

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

  • @AdamL,请记住,像其他`xsl:output`属性一样的XML声明是一个序列化属性,如果XSLT处理器确实序列化并负责序列化,那么,是的,使用正确的`xsl:output`(XML输出方法,omit-xml-declaration未明确设置为yes)它将输出XML声明.然而,有一些场景或处理器用途和API(浏览器中的`transformToDocument`)使用XSLT处理器不序列化或不负责序列化的地方,在这种情况下,`xsl:output`可能无关紧要. (2认同)