Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert xml after any element in needed order #18

Closed
marcencov opened this issue Nov 24, 2016 · 10 comments
Closed

Insert xml after any element in needed order #18

marcencov opened this issue Nov 24, 2016 · 10 comments
Labels

Comments

@marcencov
Copy link

marcencov commented Nov 24, 2016

Hi.

Can you realize to add new element after 1 or for example after 3?

For example

<modules> <module>1</module> <module>3</module> </modules>

I need to insert after 1 and before 3.

Thanks.

@martinb3
Copy link
Contributor

Hi there -- this looks possible. What XPath expression have you tried?

@marcencov
Copy link
Author

marcencov commented Nov 24, 2016

I'm not understand how realize this inserting. Can you help?

@martinb3
Copy link
Contributor

Hello! Can you show a before and after? I'm happy to help troubleshoot, but please show us what you have already tried too.

@martinb3
Copy link
Contributor

martinb3 commented Dec 7, 2016

Closing since I haven't gotten a response.

@martinb3 martinb3 closed this as completed Dec 7, 2016
@marcencov
Copy link
Author

marcencov commented Dec 8, 2016

Example.

Input: <modules><module>1</module><module>3</module></modules> (I have 1,3).

I need insert after 1, before 3 (must receive output
<modules><module>1</module><module>2</module><module>3</module></modules>
).

If I use append and prepend: false I receive: <modules><module>1</module><module>3</module><module>2</module></modules> - not correct order 1,3,2.

If I use append and prepend: true I receive: <modules><module>1</module><module>3</module><module>2</module></modules> - not correct order 2,1,3.

How I can receive 1,2,3 in correct order <modules><module>1</module><module>2</module><module>3</module></modules> (append after 1 and before 3)?

@martinb3
Copy link
Contributor

martinb3 commented Dec 8, 2016

Please show us the exact chef code you've tried 👍

@marcencov
Copy link
Author

marcencov commented Dec 14, 2016

Input xml:

<?xml version="1.0" encoding="UTF-8"?>
<Root version="2">
  <Server>
    <Modules>
    </Modules>
  </Server>
</Root>

Input chef:

include_recipe 'xmledit'

 xml_edit "test_append1" do
   path "/home/msasha/test.xml"
   parent '/Root/Server/Modules'
   target "/Root/Server/Modules/Module[Name=\'module1\']"
   fragment "<Module>
               <Name>module1</Name>
             </Module>"
   action :append_if_missing
 end

 xml_edit "test_append2" do
   path "/home/msasha/test.xml"
   parent '/Root/Server/Modules'
   target "/Root/Server/Modules/Module[Name=\'module2\']"
   fragment "<Module>
               <Name>module2</Name>
             </Module>"
   action :append_if_missing
 end

 xml_edit "test_append4" do
   path "/home/msasha/test.xml"
   parent '/Root/Server/Modules'
   target "/Root/Server/Modules/Module[Name=\'module4\']"
   fragment "<Module>
               <Name>module4</Name>
             </Module>"
   action :append_if_missing
 end

 xml_edit "test_append5" do
   path "/home/msasha/test.xml"
   parent '/Root/Server/Modules'
   target "/Root/Server/Modules/Module[Name=\'module5\']"
   fragment "<Module>
               <Name>module5</Name>
             </Module>"
   action :append_if_missing
 end

After execute I receive modules in order: module1, module2, module4, module5.
How I can insert next 'module3' after module2 and before module4?

Thanks

@martinb3
Copy link
Contributor

Have you tried something like?

 xml_edit "test_append3" do
   path "/home/msasha/test.xml"
   parent '/Root/Server/Modules'
   target "/Root/Server/Modules/Module[Name=\'module2\']"
   fragment "<Module>
               <Name>module2</Name>
             </Module>
             <Module>
               <Name>module3</Name>
             </Module>"
   action :replace
 end

@marcencov
Copy link
Author

Martin - I've run 2 times chef-client and recive next:
every time that I run chef-client this changes applied.
module3 - 2 times and more....

<?xml version="1.0" encoding="UTF-8"?> <Root version="2"> <Server> <Modules> <Module> <Name>module1</Name> </Module> <Module> <Name>module2</Name> </Module> <Module> <Name>module3</Name> </Module> <Module> <Name>module3</Name> </Module> <Module> <Name>module4</Name> </Module> <Module> <Name>module5</Name> </Module> </Modules> </Server> </Root>

@martinb3
Copy link
Contributor

martinb3 commented Dec 14, 2016

You probably need to add an only_if or not_if to that resource, to prevent insertion too many times, if you only want it to happen once. Maybe not_if 'grep module2 /home/msasha/test.xml'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants