0

I am trying to transform an XML file with an XSL file using Powershell .Net method.

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Load("C:\Users\admin\Downloads\recoveryHistory_en.xsl")

I get the below error message.

PSMessageDetails      :
Exception             : System.Management.Automation.MethodInvocationException: Exception calling "Load" with "1" argument(s): "XSLT compile error." ---> System.Xml.Xsl.XslLoadException: XSLT compile error. --->
                        System.Xml.XmlException: For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into
                        XmlReader.Create method.
                           at System.Xml.XmlTextReaderImpl.Throw(Exception e)
                           at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
                           at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
                           at System.Xml.Xsl.Xslt.XsltInput.ReadTextNodes()
                           at System.Xml.Xsl.Xslt.XsltInput.ReadNextSibling()
                           at System.Xml.Xsl.Xslt.XsltInput.FindStylesheetElement()
                           at System.Xml.Xsl.Xslt.XsltLoader.LoadDocument()
                           at System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
                           --- End of inner exception stack trace ---
                           at System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
                           at System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
                           at System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
                           at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
                           at CallSite.Target(Closure , CallSite , Object , String )
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
                           at CallSite.Target(Closure , CallSite , Object , String )
                           at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
                           at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          :
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : XslLoadException
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

I set the DTDProcessing to Parse as the error pointed out, but still no luck.

[System.Xml.XmlReaderSettings]$xml=New-Object System.Xml.XmlReaderSettings
$xml.DtdProcessing = 'Parse'
$xml


Async                        : False
NameTable                    :
XmlResolver                  :
LineNumberOffset             : 0
LinePositionOffset           : 0
ConformanceLevel             : Document
CheckCharacters              : True
MaxCharactersInDocument      : 0
MaxCharactersFromEntities    : 0
IgnoreWhitespace             : False
IgnoreProcessingInstructions : False
IgnoreComments               : False
ProhibitDtd                  : False
DtdProcessing                : Parse
CloseInput                   : False
ValidationType               : None
ValidationFlags              : ProcessIdentityConstraints, AllowXmlAttributes
Schemas                      : System.Xml.Schema.XmlSchemaSet

I spotted a lot of questions in this forum similar to mine (transforming xml to html using xslt, How can I bind tag id when convert Xml to Html with Xslt, What is the syntax for converting XML to HTML using PSCX Convert-XML?, Using PowerShell to convert XML to HTML). I tried some of them (for example: convert-xml, validating the xml file content etc.,). Does anyone anyone have a clue on what's with the error message and what could be wrong?

joso
  • 29
  • 1
  • 9
  • 1
    In C# or VB or any other .NET language you would need to create an XmlReader with those XmlReaderSettings and pass that XmlReader to the Load method of XslCompiledTransform instance. Not sure about the Powershell syntax to use a static .NET method like `XmlReader.Create("C:\Users\admin\Downloads\recoveryHistory_en.xsl", new XmlReaderSettings() { DtdProcessing = DtdProcessing.Parse })`. – Martin Honnen Dec 18 '20 at 13:43

1 Answers1

1

I think you need to try along the lines of

$xrs=New-Object System.Xml.XmlReaderSettings
$xrs.DtdProcessing = 'Parse'

$xr = [System.Xml.XmlReader]::Create("C:\Users\admin\Downloads\recoveryHistory_en.xsl", xrs)

$xslt.Load($xr)
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you, one more step closer to the solution. Creating an ```XmlReader``` and passing that ```XmlReader``` to the Load method of ```XslCompiledTransform``` instance did work. No more errors when loading. – joso Dec 18 '20 at 16:51