XML Examples

XML syntax highlighting

XML Code Example

<?xml version="1.0" encoding="UTF-8"?> <!-- Sample XML Document demonstrating various features --> <!-- Root element with namespace declaration --> <library xmlns:book="http://example.com/books" xmlns:author="http://example.com/authors"> <!-- Element with multiple attributes --> <book:collection id="scifi-classics" category="Science Fiction" year="2024"> <!-- Nested elements with different structures --> <book:item isbn='978-0-441-17271-9' format="hardcover"> <book:title>Neuromancer</book:title> <author:name>William Gibson</author:name> <book:published>1984</book:published> <!-- Entity references --> <book:description> A &quot;cyberpunk&quot; masterpiece that defined a genre. The story follows Case, a washed-up computer hacker &amp; console cowboy. </book:description> <!-- CDATA section for preserving special characters --> <book:code-snippet> <![CDATA[ if (condition < 10 && value > 5) { console.log("Special chars: <>&\"'"); } ]]> </book:code-snippet> </book:item> <book:item isbn="978-0-441-00590-0" format="paperback"> <book:title>Dune</book:title> <author:name>Frank Herbert</author:name> <book:published>1965</book:published> <book:description> Set on the desert planet Arrakis, where &quot;spice&quot; is the most valuable substance. Winner of both Hugo &amp; Nebula awards. </book:description> <!-- Self-closing tags --> <book:available/> <book:bestseller/> </book:item> </book:collection> <!-- URL encoding in attributes --> <book:reference url="http://example.com/catalog?id=123&amp;category=scifi" encoded="%20%2F%3A%3F%3D"> External catalog link </book:reference> <!-- Multi-line comment demonstrating various XML features and patterns --> <!-- Common XML entities --> <book:entities> &lt; Less than &gt; Greater than &amp; Ampersand &quot; Quote &apos; Apostrophe &nbsp; Non-breaking space </book:entities> <!-- Mixed content with both text and elements --> <book:description> This is <book:emphasis>mixed content</book:emphasis> with both text nodes and <book:strong>element nodes</book:strong> together. </book:description> <!-- Complex CDATA with nested structure --> <book:template> <![CDATA[ <html> <body> <h1>Title</h1> <p>Content with <, >, & symbols</p> </body> </html> ]]> </book:template> </library>