1

I need to run a XSLT transformation using a PowerShell script that recursively for all the object *.xml:

  • Load the file
  • Transform it
  • Save the output with the same file name in the same path.

I thought something like this

FolderToCheck = "MyFolder"
Get-ChildItem $FolderToCheck -Filter *.xml | ForEach-Object {
    # declare Xslt script for transformation
    [Xml]$Xslt_script = Get-content 'MyScript.xslt'
    # read xml
    $File = [xml](Get-Content $_.FullName)
    # Run the transformation
    $xslt = New-Object System.Xml.Xsl.XslCompiledTransform
    $xslt.Load($Xslt_script)
    $xslt.Transform($File,$File)
    }

I'm always getting error

Exception calling "Transform" with "2" argument(s): "Could not find file 'C:\Users\gdiprima00\System.Xml.XmlDocument'." At line:9 char:1 + $xslt.Transform($File,$File) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FileNotFoundException

What is it wrong? Can you suggest what to amend?

0

1 Answer 1

1

Thanks to @Martin Honnen suggestion, I have amended a little bit my code as follows:

Get-ChildItem $FolderToCheck -Filter *.xml | ForEach-Object {
    # declare Xslt script for transformation
    [Xml]$Xslt_script = Get-content 'MyScript.xsl'
    # Create Output File Name
    $dir = $_.DirectoryName
    $BaseName = $_.Basename
    $OutputFileName = $_.DirectoryName + '\' + $_.Basename + '_v2.xml'
    # Run the transformation
    $xslt = New-Object System.Xml.Xsl.XslCompiledTransform
    $xslt.Load($Xslt_script)
    $xslt.Transform($_.FullName, $OutputFileName)
    rm $_.FullName
    }

With this I'm able to generate my *_v2.xml file and then delete the original file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.