adding new new

Help
cullepm3
2005-10-25
2013-03-13
  • cullepm3
    cullepm3
    2005-10-25

    Is it possible to add a new element with attributes? I tried this but it didn't work.

    xmlstarlet  -s "/CALYPSO/CACHE/MULTIPORT" -t elem -n "LISTEN PORT=\"4000\"" -v "" blah.xml

     
    • Mikhail Grushinskiy
      Mikhail Grushinskiy
      2005-10-25

      You missed 'ed'

      Example:

      $ echo "<x/>" | xml ed --subnode /x -t elem -n new -v val --insert /x/new -t attr -n data -v test -
      <?xml version="1.0"?>
      <x>
        <new data="test">val</new>
      </x>

      And your name of the element is not right.

      --MG

       
    • cullepm3
      cullepm3
      2005-10-25

      Thanks for your help.  I have run into another problem.  When the document has a default namespace, the newly added node can't be referred to using the namespace.

      [blah.xml]

      <?xml version="1.0" encoding='ISO-8859-1'?>
      <CALYPSO xmlns="http://www.oracle.com/iAS/EMComponent/webcache">
        <CACHE>
          <MULTIPORT/>
        </CACHE>
      </CALYPSO>

      [doesn't work]

      xmlstarlet ed -N xmlns=http://www.oracle.com/iAS/EMComponent/webcache \ --subnode "/xmlns:CALYPSO/xmlns:CACHE/xmlns:MULTIPORT" -t elem -n "LISTEN" -v "" \ --insert "/xmlns:CALYPSO/xmlns:CACHE/xmlns:MULTIPORT/xmlns:LISTEN" -t attr -n PORT -v 4444 \ blah.xml

       
    • Mikhail Grushinskiy
      Mikhail Grushinskiy
      2005-10-26

      $ xml ed -N n=http://www.oracle.com/iAS/EMComponent/webcache --subnode "/n:CALYPSO/n:CACHE/n:MULTIPORT" -t elem -n "LISTEN" -v "" --insert "/n:CALYPSO/n:CA
      CHE/n:MULTIPORT/LISTEN" -t attr -n PORT -v 4444 blah.xml

      <?xml version="1.0" encoding="ISO-8859-1"?>
      <CALYPSO xmlns="http://www.oracle.com/iAS/EMComponent/webcache">
        <CACHE>
          <MULTIPORT>
            <LISTEN PORT="4444"></LISTEN>
          </MULTIPORT>
        </CACHE>
      </CALYPSO>

       
    • Mikhail Grushinskiy
      Mikhail Grushinskiy
      2005-10-26

      Previous example works and it is actually not quite right.
      It should have worked like this

      xmlstarlet ed -N xmlns=http://www.oracle.com/iAS/EMComponent/webcache \
      --subnode "/xmlns:CALYPSO/xmlns:CACHE/xmlns:MULTIPORT" -t elem -n "xmlns:LISTEN" -v "" \
      --insert "/xmlns:CALYPSO/xmlns:CACHE/xmlns:MULTIPORT/xmlns:LISTEN" -t attr -n PORT -v 4444 \
      blah.xml

      but it seems there is a bug in namespaces handling when adding a new node. It has to be added with its namespace.

      Thanks,
      --MG