Menu

Copy text from one tag to another

Help
teqie1979
2017-08-10
2017-08-11
  • teqie1979

    teqie1979 - 2017-08-10

    I need to copy, for multiple lines, the contents of <SourceTag>Text</SourceTag> to <TagToUpdate></TagToUpdate> (empty)..
    I assumed the edit-update function was the correct one. It seems to be doing something, but the result is the same as when I started. Is the "/Object/SourceTag" allowed as parameter? Most examples have a hardcoded text.

    xml ed -u "/Object/TagToUpdate" -v "/Object/SourceTag" MyXMLFile.xml

    If not allowed, what would be the solution?

     
    • Noam Postavsky

      Noam Postavsky - 2017-08-10

      On Thu, Aug 10, 2017 at 3:53 PM, Arvid Egbertzen teqie1979@users.sf.net wrote:

      I need to copy, for multiple lines, the contents of <SourceTag>Text</SourceTag> to <TagToUpdate></TagToUpdate> (empty)..
      I assumed the edit-update function was the correct one. It seems to be doing something, but the result is the same as when I started. Is the "/Object/SourceTag" allowed as parameter? Most examples have a hardcoded text.

      xml ed -u "/Object/TagToUpdate" -v "/Object/SourceTag" MyXMLFile.xml

      If not allowed, what would be the solution?

      "-v" is used to specify a string, "-x" is for an XPath expression, so
      something along the lines of

      xml ed -u "/Object/TagToUpdate" -x "/Object/SourceTag/text()" MyXMLFile.xml
      

      Also, note the -L or --inplace option if you want to edit the original file.

       
      • teqie1979

        teqie1979 - 2017-08-11

        Thanks Postavsky, but it didn't really work. Like other things I tried yesterday, it replaces my <TagToUpdate></TagToUpdate> into <TagToUpdate/>.

        By the way, as I'm a bit of a noob here, I export the result to another file.

         

        Last edit: teqie1979 2017-08-11
  • teqie1979

    teqie1979 - 2017-08-11

    Update: it seems not to collect the <SourceTag> contents. Also, If I fill in something in the <TagToUpdate>, it leaves it alone (value stays).

     
  • Noam Postavsky

    Noam Postavsky - 2017-08-11

    Works for me, maybe you typo'd a tag name? Note that <Tag/> and <Tag></Tag> are 100% identical as far as xmlstarlet is concerned.

    ~/tmp$ cat input.xml
    <Object>
      <SourceTag>Text</SourceTag>
      <TagToUpdate></TagToUpdate>
    </Object>
    ~/tmp$ xmlstarlet ed -u /Object/TagToUpdate -x '/Object/SourceTag/text()' input.xml
    <?xml version="1.0"?>
    <Object>
      <SourceTag>Text</SourceTag>
      <TagToUpdate>Text</TagToUpdate>
    </Object>
    ~/tmp$ xmlstarlet --version
    1.6.1
    compiled against libxml2 2.9.2, linked with 20904
    compiled against libxslt 1.1.28, linked with 10129-GITv1.1.29-23-geb1030d
    
     
  • teqie1979

    teqie1979 - 2017-08-15

    Thanks again Noam for your reply.

    In the end I used SED to manipulate the data. I would have liked this better, as it is a lot better to understand. I gave it another try, but it still won't work. I have the same version, but compiled for Windows. Maybe I can find a Linux box somewhere to test if it behaves differently.

    I know <Tag></Tag> and <Tag/> are both empty tags. I reported it more as a proof that it was updating the file, but it put in 'nothing'.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.