How to enable DTD in XML documents loaded using XSLT document function (uri) using XslCompiledTransform (.NET)

When a function is document(uri)used to load another document into XSLT, where the target document contains a DTD, I get XslTransformExceptionthat contains an internal one XmlException:

For security reasons, DTD is not permitted in this XML document. To enable DTD processing, set the ProhibitDtd property to XmlReaderSettings false and pass the settings to the XmlReader.Create method.

Since XmlReaderestablished in XSLT implementation, there is no direct way to create an XmlReader with the desired settings, and none XslCompiledTransform, XmlResolver 1 or related classes seem to provide any form of hook.

(I use DTD to set some HTML objects to make document creation easier.)

1 Based on a view XmlUrlResolverin Reflector, it simply loaded the target URI as a stream.

eg. XSLT contains

<xsl:apply-templates select="document('more.xml')/root"/>

and more.xmlcontains a DTD, then the above exception is the result.

+2
source share
1 answer

The one XmlResolveryou pass to the method Transformis used for the document function, this resolver can return XmlReader, which allows DTD instead of returning Stream. You can also return IXPathNavigable. In other words, to fix this problem you will need a specialized implementation XmlResolver.

+3
source

Source: https://habr.com/ru/post/1763384/


All Articles