
Perl and XML
Author: John M. Gabriele | back to index
- Quick notes on XML
- Commonly-used Perl modules
- General use
- Validating XML files against a W3C Schema
- Validating XML files against a DTD
- Finding elements in a tree
- Transforming XML files into some other format
- Links
---
My quick reference notes on using Perl with XML.
Quick notes on XML
Tiny XML refersher.
Content of an XML file
- Only one top-level element per file.
<!-- This is for comments -->- All elements have start/end tags, except ones that look like
<this/>. - Quote your attribute values (either
<with a='single'> <or b="double">quote marks). - Use
<,>, and&for <, >, and &. Also, use"and'for undirected double-quote and single-quote marks.
Structure and tags
You specify the structure and tags of your XML files with either a DTD or a schema. DTD's came from SGML. Schema's are newer.
The Perl Cookbook tells me that the W3C standard (http://www.w3.org/TR/xmlschema-0/) "XML Schema" is the most common schema language but also is complex and problematic.
You usually use a schema or a DTD for validating an xml file against.
Commonly-used Perl modules
General use
Use XML::Twig. Note, XML::Twig makes use of (actually, is a
subclass of) the standard XML::Parser, which in turn talks to the
expat C library. Don't directly use XML::Parser -- let wrappers
like Twig talk to it for you.
Also, for just writing simple XML files, XML::Simple can be quite
useful.
Validating XML files against a W3C Schema
Use XML::Xerces. (XML::LibXML can do this, but it's currently
undocumented.) See also XML::Validator::Schema.
Validating XML files against a DTD
Use XML::LibXML (which uses Gnome's libxml2). See also
XML::Checker.
Finding elements in a tree
See XML::XPath support in XML::LibXML.
Transforming XML files into some other format
See XML::LibXSLT (built on Gnome's libxslt library).
Links
- Perl-XML faq -- http://perl-xml.sourceforge.net/faq/
- Another faq -- http://xml.silmaril.ie/
XML::Twig-- http://xmltwig.com/