You can learn about writing XML with Nushell n a previous blogpost.
To add a XML comment you can use a record with the key tag and string value !.
The content key should be a string value that is the actual comment.
The result after you use the to xml is a XML text that has the format of a XML comment: <!-- content -->.
In the following example a simple XML text is created with a XML comment:
use std/assert
# Sample record
let user = {
tag: users
content: [
# Define XML comment
{
tag: '!'
content: 'Describes user name'
}
{
tag: user
content: ['mrhaki']
}
]
}
assert equal ($user | to xml -i 2) '<users>
<!--Describes user name-->
<user>mrhaki</user>
</users>'
In the following example a re-usable command is created that will add a space before and after the content in the XML comment:
use std/assert
# Command to create XML comment
# record structure and add space before and
# after the XML comment value
def xml-comment [content: string] {
# Define XML content
{
tag: '!'
// Adding space before and after content.
content: $" ($content) "
}
}
# Sample record
let user = {
tag: users
content: [
# Invoke command xml-comment
(xml-comment 'User name')
{
tag: user
content: ['mrhaki']
}
# Invoke command xml-comment
(xml-comment 'Age')
{
tag: age
content: ['53']
}
]
}
assert equal ($user | to xml -i 2) '<users>
<!-- User name -->
<user>mrhaki</user>
<!-- Age -->
<age>53</age>
</users>'
Written with Nushell 0.115.1