Applying XSL to XML with PowerShell : Exception ca

2019-08-15 04:15发布

问题:

I'm trying to clean XML Files with an XSL Transformation. When I'm applying my XSL directly to my XML File, it works fine. But now I want to clean every XML File I have in a directory. I've tried to create a PowerShell script, to load and transform XML Files.

Here is my code :

$dir = "C:\MyDirectory"
$XSLFileName = "XSLTFile.xsl"
$XSLFileInput = $dir + $XSLFileName

$XMLFileName = "Input.xml"
$XMLInputFile = $dir + $XMLFileName
$OutPutFileName = "Output.xml"
$XMLOutputFile = $dir + $OutPutFileName

cd $dir 

$XSLInputElement = New-Object System.Xml.Xsl.XslCompiledTransform;
$XSLInputElement.Load($XSLFileInput)

$XMLInputDoc = Get-Content -Path $XMLInputFile

$reader = [System.Xml.XmlReader]::Create($XMLInputFile)
$writter = [System.Xml.XmlTextWriter]::Create($XMLOutputFile)

$XSLInputElement.Transform($XMLInputDoc, $writter)

I've been threw some docs and SO subjects to find how to use the Transform() method, but I haven't found how to deal with this error :

Exception calling "Transform" with "2" argument(s): "Caractères non conformes dans le chemin d'accès."
At C:\ScirptsDirs\StackOverFlowTransformExample.ps1:20 char:1
+ $XSLInputElement.Transform($XMLInputDoc, $writter)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Note : "Caractères non conformes dans le chemin d'accès." means : Invalid symbol found in the access path.

What I want to do is to clean my XML File, and to create an other XML File but with the XSL Transformation applied to it.

EDIT : I've also tried this way, as Martin said :

$XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)

But this time, I have thee following error :

Exception calling "Transform" with "2" argument(s): "Execution of the 'document()' function was prohibited. Use the XsltSettings.EnableDocumentFunction property to enable it. An error occurred at  D:\MyDirectory\XSLTFile.xsl(220,3)."
At C:\ScirptsDirs\StackOverFlowTransformExample.ps1:20 char:1
+ $XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : XslTransformException

回答1:

If you have the input and output file name respectively path as a string then simply use $XSLInputElement.Transform($XMLInputFile, $XMLOutputFile).



回答2:

This is working for me. The extra lines are for enabling scripts in the xsl.

EDIT. Seems the solution to above was that a line to enable the document function was needed. Similar to how I enable scripts.

$xslt_settings.EnableDocumentFunction = 1;

could be added to my function

function TransformXML{
    param ($xml, $xsl, $output)

    if (-not $xml -or -not $xsl -or -not $output)
    {
        Write-Host "& .\xslt.ps1 [-xml] xml-input [-xsl] xsl-input [-output] transform-output"
        return 0;
    }

    Try
    {
        $xslt_settings = New-Object System.Xml.Xsl.XsltSettings;
        $XmlUrlResolver = New-Object System.Xml.XmlUrlResolver;
        $xslt_settings.EnableScript = 1;

        $xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
        $xslt.Load($xsl,$xslt_settings,$XmlUrlResolver);
        $xslt.Transform($xml, $output);

    }

    Catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Write-Host  'Error'$ErrorMessage':'$FailedItem':' $_.Exception;
        return 0
    }
    return 1

}