You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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:output element is the critical piece here. The doctype-system attribute directly sets the DTD filename exactly as required in your target declaration. Since your DOCTYPE uses SYSTEM (not PUBLIC), we don't need to include the doctype-public attribute (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:36:23