手を上げて!これは、間違った質問をする典型的なケースです。
問題は、 xmlpokeを使用して単一のノードを削除できないことです。Xmlpokeは、特定のノードまたは属性の内容を編集するためにのみ使用できます。標準のNantターゲットのみを使用して、質問に従って子ノードを削除するエレガントな方法はありません。これは、Nant のプロパティを使用した洗練されていない文字列操作を使用して行うことができますが、なぜそうしたいのでしょうか?
これを行う最善の方法は、単純な Nant ターゲットを作成することです。これは私が以前に準備したものです:
using System;
using System.IO;
using System.Xml;
using NAnt.Core;
using NAnt.Core.Attributes;
namespace XmlStrip
{
[TaskName("xmlstrip")]
public class XmlStrip : Task
{
[TaskAttribute("xpath", Required = true), StringValidator(AllowEmpty = false)]
public string XPath { get; set; }
[TaskAttribute("file", Required = true)]
public FileInfo XmlFile { get; set; }
protected override void ExecuteTask()
{
string filename = XmlFile.FullName;
Log(Level.Info, "Attempting to load XML document in file '{0}'.", filename );
XmlDocument document = new XmlDocument();
document.Load(filename);
Log(Level.Info, "XML document in file '{0}' loaded successfully.", filename );
XmlNode node = document.SelectSingleNode(XPath);
if(null == node)
{
throw new BuildException(String.Format("Node not found by XPath '{0}'", XPath));
}
node.ParentNode.RemoveChild(node);
Log(Level.Info, "Attempting to save XML document to '{0}'.", filename );
document.Save(filename);
Log(Level.Info, "XML document successfully saved to '{0}'.", filename );
}
}
}
上記をNAnt.exe.configファイルへの変更と組み合わせて、ビルドファイルにカスタム ターゲットと次のスクリプトをロードします。
<xmlstrip xpath="//rootnode/childnode[@arg = 'b']" file="target.xml" />
これにより、値bの引数argを持つchildnode が target.xmlから削除されます。そもそもこれが私が実際に望んでいたものです!