1

私は何百ものxmlファイルを持っていますが、特定の場所で1回限りの編集を行いたいと思っています。各xmlファイルのどこかに、次のようなものがあります。

   <SomeTag
     attribute1 = "foo"
     attribute2 = "bar"
     attribute3 = "lol"/>

属性の数とその名前はファイルによって異なりますが、変わりSomeTagません。最後の属性の後に別の属性を追加したいのですが。

この方法でxmlを編集するのはばかげていることに気づきましたが、それは私がのようなものでやりたい1回限りの仕事ですがsed、複数行の使用法を理解することはできません。

4

3 に答える 3

3

変換スタイルシートと ID テンプレート (XSLT) を使用します。

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="SomeTag">
  <xsl:copy>
    <xsl:attribute name="newAttribute">
      <xsl:value-of select="'whatever'"/>
    </xsl:attribute>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

これにより、XML 全体がコピーされますが、「SomeTag」の定義済みテンプレートが実行されます。

ここから撮影

于 2013-03-05T09:23:25.547 に答える
2

XML シェルxshを使用できます。

for my $file in { glob "*.xml" } {
    open $file ;
    for //SomeTag set @another 'new value' ;
    save :b ;
}
于 2013-03-05T09:49:52.650 に答える
1

入力ファイルが本当に単純で一貫してフォーマットされている場合:

$ cat file
foo
   <SomeTag
     attribute1 = "foo"
     attribute2 = "bar"
     attribute3 = "lol"/>
bar

$ gawk -v RS='\0' -v ORS= '{sub(/<SomeTag[^/]+/,"&\n     attribute4 = \"eureka\"")}1' file
foo
   <SomeTag
     attribute1 = "foo"
     attribute2 = "bar"
     attribute3 = "lol"
     attribute4 = "eureka"/>
bar
于 2013-03-05T15:55:55.777 に答える