使用 PowerShell 将 XSL 应用于 XML:异常调用“转换" [英] Applying XSL to XML with PowerShell : Exception calling "Transform"
问题描述
我正在尝试使用 XSL 转换来清理 XML 文件.当我将 XSL 直接应用于我的 XML 文件时,它工作正常.但是现在我想清理目录中的每个 XML 文件.我尝试创建一个 PowerShell 脚本来加载和转换 XML 文件.
这是我的代码:
$dir = "C:\MyDirectory"$XSLFileName = "XSLTFile.xsl"$XSLFileInput = $dir + $XSLFileName$XMLFileName = "Input.xml"$XMLInputFile = $dir + $XMLFileName$OutPutFileName = "输出.xml"$XMLOutputFile = $dir + $OutPutFileNamecd $目录$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)
我被扔了一些文档和 SO 主题来寻找如何使用 Transform()
方法,但我还没有找到如何处理这个错误:
使用2"个参数调用Transform"的异常:Caractères nonconformes dans le chemin d'accès."在 C:\ScirptsDirs\StackOverFlowTransformExample.ps1:20 字符:1+ $XSLInputElement.Transform($XMLInputDoc, $writter)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId : ArgumentException
<块引用>
注意:Caractères nonconformes dans le chemin d'accès."方法 :在访问路径中发现无效符号.
我想要做的是清理我的 XML 文件,并创建另一个 XML 文件,但应用了 XSL 转换.
我也试过这种方式,正如马丁所说:
$XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)
但这一次,我有以下错误:
使用2"个参数调用Transform"时出现异常:禁止执行‘document()’函数.使用 XsltSettings.EnableDocumentFunction 属性来启用它.在 D:\ 处发生错误MyDirectory\XSLTFile.xsl(220,3)."在 C:\ScirptsDirs\StackOverFlowTransformExample.ps1:20 字符:1+ $XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId : XslTransformException
如果您将输入和输出文件名分别路径作为字符串,那么只需使用 $XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)代码>.
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
If you have the input and output file name respectively path as a string then simply use $XSLInputElement.Transform($XMLInputFile, $XMLOutputFile)
.
这篇关于使用 PowerShell 将 XSL 应用于 XML:异常调用“转换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!