Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have a strange issue in XML parsing with XML::Xerces
we are trying to parse an xml file using an xsd with XML::Xerces::XercesDOMParser (version 260.2).
The corresponding xsd file has optional attributes having default values.
Once we execute the Perl script, we are seeing only the default values of optional attributes even when the attributes are having different values than the defined default value.
Below is an example of my xsd and xml
sample.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:complexType name="eventDefinitionsType"> <xs:sequence> <xs:element name="event-def" type="ems:eventDe +fType" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="eventDefType"> <xs:attribute name="name" type="eventDefNameType" use= +"required"/> <xs:attribute name="version" type="eventDefVersionType +" use="optional" default="1"/> <xs:attribute name="severity" type="eventDefSeverityTy +pe" use="required"/> </xs:complexType> <xs:attribute name="name" type="eventDefNameType" use="required"/> <xs:attribute name="severity" type="SeverityType" use="required"/> <xs:attribute name="version" type="VersionType" use="optional" defaul +t="1"/>
sample.xml <event-def name="kern_event" version="2" severity="VAR"> <description> This event is generated when a kernel syslog m +essage is detected. </description> <param name="pri" type="INT"> <description> The syslog priority of the message </description> </param> <param name="msg" type="STRING"> <description> The text of the message </description> </param> <corrective-action type="NONE"/> <since>6.0</since> <deprecated> Deprecated as of version 6.1.2 to address the +formatting problem of forwarded syslog messages. </deprecated> </event-def>
Below is the code I'm using to get the values, even the value of "version" is defined as "2" in the xml, the script gets the value as "1" which is the default for every "version" attribute in xml.
use strict; use warnings; use XML::Xerces; use Data::Dumper; my $parser = XML::Xerces::XercesDOMParser->new(); $parser->setDoNamespaces(1); $parser->setDoSchema(1); $parser->setCreateEntityReferenceNodes(1); $parser->setDisableDefaultEntityResolution(0); #$parser->setValidationScheme($XML::Xerces::AbstractDOMParser: +:Val_Always); $parser->setErrorHandler(new XML::Xerces::PerlErrorHandler()); eval { $parser->parse($dn.$xml_file_name); }; if($@) { XML::Xerces::error($@); } my $doc = $parser->getDocument(); my $event_list = $doc->getElementsByTagName('event-def'); my $event_count = $event_list->getLength(); for(my $i=0;$i<$event_count;$i++) { my $pMap = []; my $node = $event_list->item($i); my $attr_map = $node->getAttributes(); my %attr = $node->getAttributes(); print Dumper(%attr); }
Please suggest how do I get the actual value from the attribute if the attribute is defined as "optional" in the XSD or is there any bug in this version of XML::Xerces.
Thanks for your time.

Replies are listed 'Best First'.
Re: Issue with xml parsing using XML::Xerces
by dHarry (Abbot) on Jan 07, 2011 at 10:58 UTC

    Indeed a strange issue. At first glance I can't see anything wrong with your code, but than again I'm not a regular XML::Xerces user.

    The corresponding xsd file has optional attributes having default values

    This is the correct way of doing things. In fact attributes are optional by default so the you could even leave the 'use="optional"' out.

    I tried to reproduce the error (with another parser) but your xsd is not complete/incorrect. You probably have another namespace containing the types missing, i.e. where does "ems:" come from?

    Right now I can only give some general hints like: (re)think your namespaces, (default namespace?) and qualifying. Doesn't the parser produce any error messages/warnings? Are you sure your schema is correct? Can you successfully validate a xml document? Maybe you can post more complete examples of your xsd and xml.

    Harry