Background
Recently I had a need to comment out whole sections from an XML Document.
XML
Sample XML Document
DTD Internal
sample.dtd.internal.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?> <!DOCTYPE address [ <!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)> ]> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>710-181-1910</phone> </address>
DTD External
address.dtd
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>
sample.dtd.external.xml
<?XML version = "1.0" encoding = "UTF-8" standalone = "no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>650-191-9101</phone> </address>
DTD External – Adding new Element – Finance
Outline
We will trigger an error by intentionally trying to add an element ( industry ) that is not defined in our DTD file.
address.dtd
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>
sample.dtd.external.element.added.finance.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>710-181-1910</phone> <industry> Finance </industry> </address>
Output
Textual
The element ‘address’ has invalid child element ‘industry’.
Image
Explanation
Our editor thankfully informed us that the address element is not one of the allowed child elements availed under address.
DTD External – Adding new Element – Finance – Commented Out Using <!– and –>
Outline
We will comment out the industry tag.
address.dtd
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>
sample.dtd.external.element.added.finance.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>710-181-1910</phone> <!-- <industry> Finance </industry> --> </address>
DTD External – Adding new Element – Finance – Commented Out Using Nested Comments
Outline
We will complicate our pathway by attempting to use nested comments.
address.dtd
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>
sample.dtd.external.element.added.finance.commented.nested.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>710-181-1910</phone> <!-- <!-- <industry> Finance </industry> --> --> </address>
Output
Textual
Character sequence ‘–‘ is illegal inside XML comments.
Image
Explanation
The XML parser tripped on nested comments.
DTD External – Adding new Element – Finance – Commented Out Using ‘Unused Processing Instruction’
Outline
Unused processing instruction allows more complex processing.
address.dtd
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>
sample.dtd.external.element.added.finance.commented.unusedProcessingInstruction.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tom Peters</name> <company>Fidelity Investments</company> <phone>710-181-1910</phone> <!-- Surround the section with an unused processing instruction Starting the comment with <?comment --> <?comment <!-- <!-- <industry> Finance </industry> --> --> ?> <!-- Surrounded the section with an unused processing instruction Ending the comment with ?> --> </address>
Commending
The Impaler
Big gratitude to “The Impaler”.
Image
Text
You shouldn’t do it, I know. Per the XML specification you aren’t supposed to.
Having said that… I really needed it badly, so here’s the trick. Surround the section you want to comment with an unused processing instruction:
<some-tags />
<?comment
<!– traditional comment 1 –>
<badtag prop1=”123″>
<!– traditional comment 2 –>
</badtag>
<!– traditional comment 3 –>
?>
<rest-of-my-tags />
You can use any processing instruction that’s not in use. I chose “comment” as the unused processing instruction, so I started the big comment with <?comment and ended it with ?>.
Pretty much any word will do as a processing instruction since they aren’t really used most of the time. I mean, when was the last time you used one?
Environment
Unfortunately, the plugin that I am using with Notepad++ failed me miserably.
Thankfully, Microsoft’s Visual Studio bailed me out.
Source Code
GitHub
DanielAdeniji/XMLComment