Jaimy Porter
Jaimy Porter

Reputation: 3

Ignore doctype DTD but keep it in the transformed XML C#

I need to transform an XML file which has this doctype in the header:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DOC SYSTEM "ts.dtd">
<?xml-stylesheet type="text/css" href="ts.css"?>

Transformation is going fine but the doctype is gone after the XSL transforming, I have tried all sorts of settings on the XmlReaderSettings but can't get it to work.

This is my code:

XslCompiledTransform xctTransformer = new XslCompiledTransform();
xctTransformer.Load(fiXSL.FullName);

XmlReaderSettings xrsReaderSettings = new XmlReaderSettings();
xrsReaderSettings.ConformanceLevel = ConformanceLevel.Document;
xrsReaderSettings.DtdProcessing = DtdProcessing.Ignore;
xrsReaderSettings.XmlResolver = null;
xrsReaderSettings.CloseInput = true;
xrsReaderSettings.IgnoreWhitespace = false;

XmlWriterSettings xwsWriterSettings = new XmlWriterSettings();
xwsWriterSettings.Indent = true;

XmlReader xrReader = XmlReader.Create(fiXML.FullName, xrsReaderSettings);
XmlWriter xwWriter = XmlWriter.Create(fiXML.FullName.Substring(0, fiXML.FullName.Length - 4) + "_TRANS.xml", xwsWriterSettings);

// Transform the XML using the XSL
xctTransformer.Transform(xrReader, xwWriter);
xctTransformer = null;

// Close reader + writer
xrReader.Close();
xwWriter.Close();

Upvotes: 0

Views: 1080

Answers (2)

Jaimy Porter
Jaimy Porter

Reputation: 3

Another answer I just found out is textually adding it via the XSL this way:

<xsl:template match="/">
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE DOC SYSTEM "ts.dtd"&gt;</xsl:text>
    <xsl:copy>
        <xsl:apply-templates select = "*|@*|comment()|processing-instruction()|text()"/>
    </xsl:copy>
</xsl:template>

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167571

A DOCTYPE node is not part of the XSLT/XPath data model so you can't process and copy it with XSLT. If you want your final result to have a certain DOCTYPE declaration then the only way with XSLT is to make sure you have an xsl:output in your stylesheet with the doctype-system="ts.dtd".

Upvotes: 1

Related Questions