You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Bash脚本遍历目录批量执行XSLT转换(Cygwin环境)

How to Batch Convert metadata.xml Files with XSLT in Cygwin (For Total Newbies)

Hey there! I get that scripting can feel totally overwhelming when you're just starting out, so let's break this down step by step to get your batch XSLT conversion done smoothly in Cygwin.

First, Make Sure You Have xsltproc Installed

Before we dive in, we need to confirm xsltproc is available in your Cygwin environment. Open the Cygwin terminal and run:

xsltproc --version

If you get an error saying the command isn't found, fire up the Cygwin setup.exe, search for the libxslt package, and install it—this will add xsltproc to your system.

Create a Simple Batch Script

We'll write a bash script to automate finding all those metadata.xml files and converting them. Here's exactly what to do:

  1. Open the Cygwin terminal and navigate to the folder where your translation.xslt is stored (this should also be the parent folder of your data directory, unless you want to adjust paths later).
  2. Create a new script file using a beginner-friendly editor like nano:
    nano convert_metadata.sh
    
  3. Paste the following code into the editor:
    #!/bin/bash
    
    # Update this path if your translation.xslt is in a different folder
    XSLT_FILE="translation.xslt"
    
    # Loop through every subfolder inside the "data" directory
    for dir in data/*/; do
        # Grab the name of the folder (without the full path)
        folder_name=$(basename "$dir")
        # Define the path to the metadata.xml file we want to process
        metadata_path="$dir/metadata.xml"
    
        # Check if the metadata.xml file exists before trying to convert it
        if [ -f "$metadata_path" ]; then
            echo "Processing $metadata_path..."
            # Run the XSLT conversion and save output as [folder_name].rdf
            xsltproc "$XSLT_FILE" "$metadata_path" > "${folder_name}.rdf"
            echo "Successfully created ${folder_name}.rdf!"
        else
            echo "Warning: $metadata_path doesn't exist — skipping this folder."
        fi
    done
    
  4. Save the file by pressing Ctrl+O, hit Enter to confirm the filename, then exit nano with Ctrl+X.

Make the Script Executable and Run It

Now we need to give the script permission to run. In the terminal, run:

chmod +x convert_metadata.sh

Then execute the script with:

./convert_metadata.sh

Quick Tips for Newbies

  • If your data folder isn't in the same directory as the script, replace data/*/ with the full path to the data folder (e.g., /home/yourusername/documents/data/*/).
  • If translation.xslt is stored elsewhere, update the XSLT_FILE line to its full path (e.g., /home/yourusername/stylesheets/translation.xslt).
  • All the generated .rdf files will save in the same directory where you run the script.

内容的提问来源于stack exchange,提问作者Joseph Shea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:03:31