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.

In reply to Issue with xml parsing using XML::Xerces by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.