うまく使いこなせる方法Nantのxmlpoke対象ノードを削除するには

StackOverflow https://stackoverflow.com/questions/537988

  •  22-08-2019
  •  | 
  •  

質問

では、以下のxml:

<rootnode>
   <childnode arg="a">Content A</childnode>
   <childnode arg="b">Content A</childnode>
</rootnode>

を使用 XMLPoke 以下のXPath:

rootnode/childnode[arg='b']

その結果の場合の置換文字列は空です):

<rootnode>
   <childnode arg="a">Content A</childnode>
   <childnode arg="b"></childnode>
</rootnode>

の内容をchildnodeを除いて、実際にはchildnode自体は削除されます。ご希望の結果は:

<rootnode>
   <childnode arg="a">Content A</childnode>
</rootnode>

のchildnodeを選択する必要があのchildnodeの引数。

役に立ちましたか?

解決

私は自分の手!このクラシックの場合の間違いに質問です。

問題はご利用できません xmlpoke を削除するには単一ノードです。 Xmlpoke できるセルを編集するために使用される内容の特定のノードまたは属性。はありませんこれまでにない削除する子ノードとしての質問のみを使用し、標準 Nant ます。で ができ をベースに制御を行っています。あinelegant文字列操作用特性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" />

この childnode 引数の arg 価値 b から target.xml.だったのに!

他のヒント

xmlpeek のみを読み込み数値です。xmlpoke 限定値です。残念ながら、nantない xmldelete 課題です。

私は、この課題をnant <target /> にnantファイルが容易にできま再利用とプロジェクト.

と思いレバレッジnantの <regex />, <echo />, <include /><call /> 事ができます。

メリット:

  • 作品の正規表現(短所).
  • 一致するテキストを含むXML!

デメリット:

  • する問題a regex?そして現在でも問題なし!-
  • のregex値のnantファイル 必要 き逃!(オンライン-xml escaperす。
<!-- This file should be included before being called -->
<project name="YourProject">

    <target name="RemoveLineFromFile">

        <loadfile 
            file="${delete.from.file.path}" 
            property="xml.file.content" />
        <property 
            name="delete.from.file.regex" 
            value="(?'BEFORE'[\w\s\W]*)\&lt;add.*key=\&quot;AWSProfileName\&quot;.*value=\&quot;comsec\&quot;.*\/\&gt;(?'AFTER'[\w\s\W]*)" />
        <regex 
          input="${xml.file.content}" 
          pattern="${delete.from.file.regex}" />
        <echo 
          file="${delete.from.file.path}"
          message="${BEFORE}${AFTER}"
          append="false" />

    </target>

</project>

以下の方法などがあります。このすべてのパラメータはグローバル化しており、定義されている必要があ の呼び出します。

  • delete.from.file.path:パスファイルのファイルを調べるために使うことができます
  • delete.from.file.regex:の正規表現マッチングに何を削除(定義前後のグループ)
<project name="YourProject">

    <include buildfile="path\to\nant\target\RemoveLineFromFile.build"/>

    <target name="OtherWork">

        <property name="delete.from.file.path" value="path\to\xml\file.xml" />
        <property name="delete.from.file.regex" value="(?&apos;BEFORE&apos;[\w\s\W]*)\&lt;childnode.*arg=&quot;b&quot;&gt;(.*?)\&lt;\/childnode\&gt;(?&apos;AFTER&apos;[\w\s\W]*)" />
        <call target="RemoveLineFromFile" />

    </target>

</project>

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top