VS Code中如何保存XSL转换生成的无标题文件或指定输出文件名?
解决方案
一、通过VS Code任务将无标题转换结果保存为临时文件
你可以配置VS Code任务(tasks.json),自动把生成的“Untitled-1”文件保存为预设名称的临时文件,供外部工具后续调用。具体操作如下:
- 打开命令面板(Ctrl+Shift+P),选择「Tasks: Open User Tasks」或「Tasks: Open Workspace Tasks」,创建/编辑tasks.json文件。
- 根据你的操作系统添加对应任务配置:
Windows(PowerShell)
{ "version": "2.0.0", "tasks": [ { "label": "XSL转换并保存输出", "type": "shell", "command": "powershell -Command \"$content = Get-Content '${workspaceFolder}/Untitled-1' -Raw; $content | Out-File '${workspaceFolder}/temp-xslt-output.xml' -Encoding utf8\"", "dependsOn": "XSL Transform", // 替换为你实际触发XSL转换的任务/命令 "problemMatcher": [] } ] }
macOS/Linux(Bash)
{ "version": "2.0.0", "tasks": [ { "label": "XSL转换并保存输出", "type": "shell", "command": "cp \"${workspaceFolder}/Untitled-1\" \"${workspaceFolder}/temp-xslt-output.xml\"", "dependsOn": "XSL Transform", // 替换为你实际触发XSL转换的任务/命令 "problemMatcher": [] } ] }
若“Untitled-1”不在工作区根目录,可调整路径;也可使用VS Code内置命令指定保存路径,示例配置:
{ "version": "2.0.0", "tasks": [ { "label": "保存XSL转换结果", "type": "shell", "command": "code --wait --command workbench.action.files.saveAs \"${workspaceFolder}/temp-xslt-output.xml\"", "dependsOn": "XSL Transform", "problemMatcher": [] } ] }
二、使用支持指定输出文件名的XSLT扩展
如果不想通过额外任务处理,可替换为支持直接指定输出文件的XSLT扩展,推荐两个选项:
- XML Tools:内置XSLT转换功能,触发转换时可直接选择将结果保存到指定文件,无需生成无标题临时文件。
- XSLT Runner:支持在配置中预设输入XML、XSL文件路径及输出文件路径,一键完成转换并写入指定文件,适配自动化工作流。
内容的提问来源于stack exchange,提问作者Joesse
相关产品推荐
相关产品推荐

