0

I have an xml file in which I expect to find some particular tags with values that I want to use to build an url. I would like to:

  1. find these values
  2. make an url with them
  3. insert this url at the beginning of a file

For example, given xml file starting with:

<?xml version="1.0" encoding="utf-8"?>
<file_1_doc>
    <biul>1</biul>
    <poz>33792</poz>          
    <date_pub>2015-02-16</date_pub>

(...)

I want to produce an url in the base form:

http://foo.com/index.php?biul=show&position=[poz]&publdate=[date_pub]

having for the example above:

http://foo.com/index.php?biul=show&position=33792&publdate=2015-02-16
Marta Karas
  • 4,967
  • 10
  • 47
  • 77
  • 1
    Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. – Ingo Karkat Feb 16 '15 at 12:48
  • I agree with @Ingo Karkat pointing out that I did not include any evidence of my previous effort. I am new to `vim` and by this description I wanted to stress my aim of regexing text to make it a variable (the exact way I would like to use it). I have been serching the web for help but, for the lack of experience, I was not succesful of make use of hints such "just try this method" if there was no example provided. I guess the conclusion is I wanted the result to quickly as for my expercience, unfortunately. – Marta Karas Feb 22 '15 at 21:14

1 Answers1

4
  1. find these values

You need an XML parser; Vim cannot do this properly (unless the XML is always consistently structured and doesn't use specials like XML named entities, where you may attempt to parse using regular expression (matchstr()), but read this first).

Vim can embed various scripting languages, e.g. Python (:help if_pyth.txt). With their default libraries, XML parsing shouldn't be a big problem. Else, you can invoke an external tool (e.g. xmlstar) via system().

  1. make an url with them

Once you've got the parsed XML in Vim variable(s), this can be comfortably done via printf().

  1. insert this url at the beginning of a file

Again, various easy ways. For a completely new line, :call append(); to add to a line, either :execute 'normal! a' . text or the lower-level call setline(, getline(1) . ...).

Community
  • 1
  • 1
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thank you @Ingo Karkat for your attention! By the time I have learned how to make use of `matchstr()` properly, I realised in my case making a few replace actions is going to work, too. I also thank you for asnwering despite your thoughts of me not having put enough effort in this issue at first. – Marta Karas Feb 22 '15 at 21:28