Posted to tcl by mjanssen at Mon Aug 08 19:07:29 GMT 2022view raw

  1. As suggested by de:
  2.  
  3.  
  4. ### Using `createElementNS`
  5.  
  6. ```tcl
  7. package require tdom
  8.  
  9. set xml {
  10. <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com">
  11. <ns1:Item>Here be text</ns1:Item>
  12. </ns1:Root>}
  13.  
  14. dom parse $xml doc
  15. set root [$doc childNodes]
  16. set item [$doc createElementNS http://some.otherns.com ns1:Item]
  17. set text [$doc createTextNode "other text"]
  18. $item appendChild $text
  19. $root appendChild $item
  20. puts [$root asXML]
  21. ```
  22.  
  23.  
  24. ``` xml
  25. <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com/">
  26. <ns1:Item>Here be text</ns1:Item>
  27. <ns1:Item>other text</ns1:Item>
  28. </ns1:Root>
  29. ```
  30.  
  31.  
  32. ### Using `appendFromScript`
  33.  
  34. ```tcl
  35. package require tdom
  36. set xml {
  37.  
  38. <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com">
  39. <ns1:Item>Here be text</ns1:Item>
  40. </ns1:Root>}
  41.  
  42. dom parse $xml doc
  43. dom createNodeCmd -tagName ns1:Item -namespace http://some.otherns.com element my_Itemcmd
  44. dom createNodeCmd textNode t
  45.  
  46. set root [$doc childNodes]
  47. $root appendFromScript {
  48. my_Itemcmd {t Othertext}
  49. }
  50. puts [$root asXML]
  51. ```
  52.  
  53.  
  54. ```xml
  55. <ns1:Root xmlns="http://default.com/" xmlns:ns1="http://some.otherns.com/">
  56. <ns1:Item>Here be text</ns1:Item>
  57. <ns1:Item>Othertext</ns1:Item>
  58. </ns1:Root>
  59. ```
  60.