Can I add an XML declaration to an XSLT output?

2020-04-09 16:33发布

问题:

Here is my current XML output:

    <EmployeeImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
    <EmployeeNumber>123</EmployeeNumber>
    <FirstName>Jose</FirstName>
    </Employee>
    </EmployeeImport>

and so on. What I would like to have my output is the following:

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

I would like to be able to add the first line to my output. In my XSLT, I have tried &lt;

to print "<" but it is interpreted just as &lt;. I have also tried <xsl:text>, but ran into the same issue. Is there a way to add this declaratory line to my XSLT?

回答1:

Just use xsl:output.

Example...

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


标签: xml xslt