如何使用XSLT将普通XML转换为带DOCTYPE的XML文件
Add Required DOCTYPE Declaration to XML Using XSLT
To convert your regular XML into one with the specified DOCTYPE, you just need to tweak your XSLT to properly configure the output settings and preserve your original content. Here's the complete, working XSLT code:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Output configuration to inject the required DOCTYPE --> <xsl:output method="xml" doctype-system="3B3_MS_R01_00_ShipmentStatusNotification.dtd" encoding="iso-8859-1" indent="yes" /> <!-- Indent is optional but improves readability --> <!-- Identity template: copies all input content to output unchanged --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:transform>
Key Details:
- The
xsl:outputelement is the critical piece here. Thedoctype-systemattribute directly sets the DTD filename exactly as required in your target declaration. Since your DOCTYPE usesSYSTEM(notPUBLIC), we don't need to include thedoctype-publicattribute (you can leave it empty if you want to explicitly declare it, but it's not necessary). - The identity template ensures every part of your original XML—elements, attributes, text, comments—gets copied over without modification. This means your core content stays intact while the DOCTYPE is automatically added at the top of the output file.
When you run this transformation, your output XML will start with:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE Pip3B3ShipmentStatusNotification SYSTEM "3B3_MS_R01_00_ShipmentStatusNotification.dtd">
followed by your original XML content.
内容的提问来源于stack exchange,提问作者nyfin paul
相关产品推荐
相关产品推荐

