简体   繁体   中英

Transforming XML to HTML using Powershell .Net method

I am trying to transform an XML file with an XSL file using Powershell.Net method. Here's my code,

#-- Create transformation --#
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform

#-- Create a reader with DTDparsing set to parse --#
$xrs = New-Object System.Xml.XmlReaderSettings
$xrs.DtdProcessing = 'Parse'

#-- Load the XSL transform with the reader setting --#
$xr = [System.Xml.XmlReader]::Create("C:\Users\admin\Downloads\recoveryHistory_en.xsl", $xrs)
$xslt.Load($xr)

#-- Create a writer --#
$xws = New-Object System.Xml.XmlWriterSettings
$xw = [System.Xml.XmlWriter]::Create("C:\Users\admin\Downloads\Test RP_failover.html", $xslt.OutputSettings)

#-- Execute the transform and output the results to a file --#
$xslt.Transform("C:\Users\admin\Downloads\Test RP_failover.xml", $xw)
$xw.Close()

Although the $xslt.Transform() did create the HTML file as intended, it appears to be totally empty. Here are the xsl:output and xsl:choose elements in the xsl file. My XML and XSL files are stored in the link above. I wonder if the xsl:choose element in the XSL file is causing the output HTML file to be empty as the XSL has <xsl:when test="@outputType = 'csv'"> as well.

<xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>

<xsl:template match="RecoveryPlanStepReport">
<xsl:choose>
   <xsl:when test="@outputType = 'html'">
      <xsl:call-template name="PrintRecoveryPlanStepReportForHtml"/>
   </xsl:when>
   <xsl:when test="@outputType = 'csv'">
      <xsl:call-template name="PrintRecoveryPlanStepReportForCsv"/>
   </xsl:when>
</xsl:choose>
</xsl:template>

Is there a value in the Transform property of System.Xml.Xsl.XslCompiledTransform I could set to invoke <xsl:when test="@outputType = 'html'"> explicitly? I'm trying to find out a way to force the XSL to use only HTML.

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Transform.Value
OverloadDefinitions
-------------------
void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.XmlWriter results)
void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results)
void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results)
void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results)
void Transform(System.Xml.XmlReader input, System.Xml.XmlWriter results)
void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results)
void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results)
void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results)
void Transform(string inputUri, System.Xml.XmlWriter results)
void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results)
void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results)
void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results)
void Transform(string inputUri, string resultsFile)
void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver documentResolver)
void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver documentResolver)

Here's how I fixed it,

I modified the below snippet in the XSL from this

   <xsl:template match="RecoveryPlanResult">
     <xsl:choose>
       <xsl:when test="Results">  <!-- SRM 4.x identifying tag -->
         <xsl:choose>
           <xsl:when test="@outputType = 'html'">
             <xsl:call-template name="LegacyPrintHistoryReportForHtml"/>
           </xsl:when>
           <xsl:when test="@outputType = 'csv'">
             <xsl:call-template name="LegacyPrintHistoryReportForCsv"/>
           </xsl:when>
         </xsl:choose>
       </xsl:when>
       <xsl:otherwise>  <!-- SRM 5.x -->
         <xsl:choose>
           <xsl:when test="@outputType = 'html'">
             <xsl:call-template name="PrintHistoryReportForHtml"/>
           </xsl:when>
           <xsl:when test="@outputType = 'csv'">
             <xsl:call-template name="PrintHistoryReportForCsv"/>
           </xsl:when>
         </xsl:choose>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

to this

<xsl:template match="RecoveryPlanResult">
    <xsl:call-template name="PrintHistoryReportForHtml"/>
</xsl:template>

Problem: Although the match element had the correct pattern in the xsl:template element, the expression in the xsl:when element was incorrect

<xsl:when test="@outputType = 'html'">

XML

<?xml version="1.0" encoding="UTF-8"?>
<RecoveryPlanResult>
  <RecoveryPlan>
    <Name>Test RP</Name>
    <Description />
    <LocalVcAddress>LocalVC</LocalVcAddress>
    <LocalSiteName>LocalSite</LocalSiteName>
    <RemoteVcAddress>remote</RemoteVcAddress>
    <RemoteSiteName>remote</RemoteSiteName>
    <TimeZoneOffset>0</TimeZoneOffset>
  </RecoveryPlan>
</RecoveryPlanResult>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM