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"/>
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.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>
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.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); }
In reply to Issue with xml parsing using XML::Xerces by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |