I have an XML document which I need to convert to another type of XML. Some of the information cannot be encoded in the XML I want to convert to, so I want to store the extra information as comments. However, I cannot figure out how to create a comment node in XML::Twig. Does anyone know how?
A comment is just a regular element with a tag name '#COMMENT
'. So you create a comment with XML::Twig::Elt->new( '#COMMENT', ' I am a comment ');
or $elt_to_comment_on->insert_new_elt( before => '#COMMENT', ' look Ma! A comment ')
A couple of remarks:
when you process XML with XML::Twig and you want to be able to access comments, you should create the twig with the
comments -> "process"
option. Otherwise comments are not generated as full-fledged elements, but are attached to the closest element. This makes it harder to deal with them (but often makes it easier not to be tripped by comments popping up in unexpected places);have you considered using processing instructions instead of comments? The two are very similar, but PIs are generally used for passing information to other applications.
© 2022 - 2024 — McMap. All rights reserved.
<fo:block font-family="Helvetica,Symbol,ZapfDingbats,Arial Unicode" font-size="10pt" font-weight="bold" line-height="12pt" padding-bottom="0.5mm" text-align="center">ABC</fo:block>
? The#COMMENT
will not work here. – Interfertile