0

31,000 個の xml ファイルのフォルダーがあり、各ファイルの先頭にスタイルシートへの参照を追加する必要があります。

各ファイルを開き、先頭にコード行を追加し、保存して次のファイルに移動するプログラム的な方法はありますか?

4

3 に答える 3

1

クリーンとは、XML 対応のツールを使用することです。

<!-- save as add_stylesheet.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!-- at the top level of the document... --> 
    <xsl:template match="/">
        <!-- unless a stylesheet reference already exists -->
        <xsl:if test="not(processing-instruction('xml-stylesheet'))">
            <!-- ...create the stylesheet reference --> 
            <xsl:processing-instruction name="xml-stylesheet">
                <xsl:text>type="text/xsl" href="foo.xsl"</xsl:text>
            </xsl:processing-instruction>
            <xsl:text>&#xA;</xsl:text>
        </xsl:if>

        <!-- ...then copy the rest of the document --> 
        <xsl:apply-templates select="node() | @*" />
    </xsl:template>
</xsl:stylesheet>

そして、Windowsの下で

for /F "delims=" %f in ('dir /b *.xml') do msxsl "%f" add_stylesheet.xsl -o "%f"

これは、Microsoft から無料で入手できる msxsl.exe を使用します

このコマンドは元のファイルを上書きすることに注意してください。-o "output\%f"代わりに、別のディレクトリ (この場合は「出力」) にファイルを書き込むために使用します。


Linux でも同じことが機能しますが、コマンド ラインは異なります。

find . -type f -name \*.xml -exec xsltproc -o '{}' add_stylesheet.xsl '{}' \;

を使用-o './output/{}'すると、ここでもファイルが上書きされなくなります。

于 2013-10-09T15:25:37.203 に答える
1

Unix ライクなシステムを使用している場合、これは BASH for ループとcatツールを使用して行うのが非常に簡単です。

各 XML ファイルの先頭に追加する行を含む "header.txt" ファイルがあるとします。ループは次のようになります。

for file in *.xml
do
  cat header.txt $file > ${file}_new
  mv ${file}_new $file
done
于 2013-10-09T14:59:18.100 に答える
0

Windows を使用していて、各ファイルの先頭に新しい行を追加する場合は、次のようにします。

いくつかのサンプル ファイルでテストします。

@echo off
for %%a in (*.xml) do (
  >"%%a.tmp" echo ^<new header/^>
  type "%%a" >> "%%a.tmp"
)
del *.xml
ren *.tmp *.xml
于 2013-10-11T04:04:03.593 に答える