Posted to tcl by mjanssen at Mon Aug 08 19:07:29 GMT 2022view pretty
As suggested by de: ### Using `createElementNS` ```tcl package require tdom set xml { <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com"> <ns1:Item>Here be text</ns1:Item> </ns1:Root>} dom parse $xml doc set root [$doc childNodes] set item [$doc createElementNS http://some.otherns.com ns1:Item] set text [$doc createTextNode "other text"] $item appendChild $text $root appendChild $item puts [$root asXML] ``` ``` xml <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com/"> <ns1:Item>Here be text</ns1:Item> <ns1:Item>other text</ns1:Item> </ns1:Root> ``` ### Using `appendFromScript` ```tcl package require tdom set xml { <ns1:Root xmlns="http://default.com" xmlns:ns1="http://some.otherns.com"> <ns1:Item>Here be text</ns1:Item> </ns1:Root>} dom parse $xml doc dom createNodeCmd -tagName ns1:Item -namespace http://some.otherns.com element my_Itemcmd dom createNodeCmd textNode t set root [$doc childNodes] $root appendFromScript { my_Itemcmd {t Othertext} } puts [$root asXML] ``` ```xml <ns1:Root xmlns="http://default.com/" xmlns:ns1="http://some.otherns.com/"> <ns1:Item>Here be text</ns1:Item> <ns1:Item>Othertext</ns1:Item> </ns1:Root> ```