如何用"set"创建分离的XML节点.在木偶中使用Augeas吗? [英] How to create separated XML nodes with "set" in Puppet using Augeas?

查看:106
本文介绍了如何用"set"创建分离的XML节点.在木偶中使用Augeas吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Puppet 3.2的Augeas工具,并且正在尝试创建XML文件.我希望能够将多个具有相同名称的字段添加到我的XML文档中.例如,我想将node2/location2与node1/location1分开,方法是将其放置在其自己的"node"字段中.这是我的代码:

I am using the Augeas tool for Puppet 3.2 and I am trying to create an XML file. I want to be able to add multiple fields with the same name into my XML doc. For instance, I want to separate node2/location2 from node1/location1 by placing it in its own "node" field. This is my code:

    augeas { "update template":
        lens    => "Xml.lns",
        require => File["${buildpath}/tempfile.xml"],
        incl => "${buildpath}/tempfile.xml",
        changes => [
            "set member/acceptors[#attribute]/node[#attribute]/nodeIdentity[#attribute]/#text node2",
            "set member/acceptors/node/nodeLocation[#attribute]/#text location2",
            "set member/acceptors/node/nodeIdentity[#attribute]/#text node1",
            "set member/acceptors/node/nodeLocation[#attribute]/#text location1"
        ],
   }

这是我得到的XML输出:

This is the XML output that I get:

    <member>
        <acceptors>
            <node>
                <nodeIdentity>node2</nodeIdentity>
                <nodeLocation>location2</nodeLocation>
                <nodeIdentity>node1</nodeIdentity>
                <nodeLocation>location1</nodeLocation>
            </node>
        </acceptors>
    </member>

这是我想要的输出:

    <member>
        <acceptors>
            <node>
                <nodeIdentity>node2</nodeIdentity>
                <nodeLocation>location2</nodeLocation>
            </node>
            <node>
                <nodeIdentity>node1</nodeIdentity>
                <nodeLocation>location1</nodeLocation>
            </node>
        </acceptors>
    </member>

我尝试将[#attribute]添加到node1行,如下所示:

I have tried adding [#attribute] to the node1 line like the following:

     "set member/acceptors/node[#attribute]/nodeIdentity[#attribute]/#text node1",

但是不会输出"node1".有什么建议吗?

But "node1" doesn't get outputted. Any suggestions?

推荐答案

您需要指定要影响XPath表达式的node.就您而言,您可以通过执行以下操作来写幂等更改:

You need to specify the node you want to impact with the XPath expression. In your case, you can write an idempotent change by doing this:

augeas { "update template":
     lens    => "Xml.lns",
     require => File["${buildpath}/tempfile.xml"],
     incl    => "${buildpath}/tempfile.xml",
     changes => [
         "set member/acceptors/node[nodeIdentity/#text='node2']/nodeIdentity/#text node2",
         "set member/acceptors/node[nodeIdentity/#text='node2']/nodeLocation/#text location2",
         "set member/acceptors/node[nodeIdentity/#text='node1']/nodeIdentity/#text node1",
         "set member/acceptors/node[nodeIdentity/#text='node1']/nodeLocation/#text location1"
     ],
}

(我看到)不需要过滤#attribute子节点的存在,更不用说不创建子节点了,所以您所做的更改不会是幂等的.

There is no need (that I see) to filter on the existence of #attribute sub-nodes, all the more that you don't create them, so you're changes won't be idempotent.

这篇关于如何用"set"创建分离的XML节点.在木偶中使用Augeas吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆