XML ファイルの編集に問題があります。私は現在Nokogiriを使用しようとしていますが、この問題を解決するために他の Ruby ライブラリを使用できます。
別のノード セット内にノード セットを追加しようとしています。どちらにも興味深い名前空間があります。これがコードです。最初の直後に new_node を親に追加しようとしています<p:sp>
require 'rubygems'
require 'nokogiri'
parent = <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
<p:spTree>
<p:sp>
<p:nvSpPr>
<p:cNvPr id="1" name="Title 1"/>
</p:nvSpPr>
</p:sp>
</p:spTree>
</p:sld>
EOF
new_node = <<EOF
<p:sp>
<p:cNvPr id="2" name="Title 2"/>
<a:off x="1524000" y="4572000"/>
</p:sp>
EOF
@doc = Nokogiri::XML(parent)
@doc.xpath('.//p:sp').after(new_node)
上記のコードを実行すると、@doc は以下の XML のようになります。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
<p:spTree>
<p:sp>
<p:nvSpPr>
<p:cNvPr id="1" name="Title 1"/>
</p:nvSpPr>
</p:sp>
<p:p:sp>
<p:p:cNvPr name="Title 2" id="2"/>
<p:a:off x="1524000" y="4572000"/>
</p:p:sp>
</p:spTree>
</p:sld>
p: の下のすべての名前空間が再度指定されていることに注意してください。2つのノードはそうである必要があり<p:sp>
、p :をnew_nodeから削除することもできますが、a:offはp:の下に名前空間が付けられたままになり、それはできません。私は何か間違ったことをしているに違いないことを知っています。私が探している最終結果は次のとおりです。<a:off>
<p:p:sp>
<p:a:off>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
<p:spTree>
<p:sp>
<p:nvSpPr>
<p:cNvPr id="1" name="Title 1"/>
</p:nvSpPr>
</p:sp>
<p:sp>
<p:cNvPr name="Title 2" id="2"/>
<a:off x="1524000" y="4572000"/>
</p:sp>
</p:spTree>
</p:sld>