[Xml-compile] Unexpected elements and attributes

John Hayden john.hayden at analog.com
Wed Apr 14 16:42:38 GMT 2010


HI all,
I have three very basic questions, as a new user of XML::Compile.

1) how can I get the compiled reader to flag elements that I haven't 
declared in my schema?
2) similarly, how can I get it to flag undeclared attributes?
3) last, why can't I declare my element's attributes before its child 
elements?
<whine> It's a pain in a large schema to have to look for the attributes 
way down after all of the elements. When I read the XML, the attributes 
of course appear first within the start tag, followed by the child 
element structure. I'd like the schema to read the same way if possible. 
</whine>

Here's my simple example. I'd like the unexpected badAttr and <bad> 
elements to cause an error.
Basically, I'd like to turn on strict checking somehow.
Thanks,
/John Hayden

-------------------------------------------
#! /usr/local/bin/perl -w
use strict;
use XML::Compile::Schema;
use Data::Dumper;

my $SCHEMA = <<'_SCHEMA_' ;
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="top">
    <xsd:complexType>
      <!-- would really like to declare attributes here, not down below -->
      <xsd:sequence>
    <xsd:element name="bottom" type="xsd:string"/>
    <!-- element bad not allowed -->
      </xsd:sequence>
      <xsd:attribute name="goodAttr"  type="xsd:string"/>
      <!-- attribute badAttr not allowed -->
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
_SCHEMA_

my $XML = <<'_XML_' ;
<top goodAttr="good" badAttr="bad">
  <bottom>
    some text
    <bad>badElement</bad>
  </bottom>
</top>
_XML_

my $schema = XML::Compile::Schema->new($SCHEMA);
my $reader = $schema->compile(READER => 'top');
my $data   = $reader->($XML);
print Dumper($data);
-------------------------------------------
This script produces the following output:
$VAR1 = {
          'goodAttr' => 'good',
          'bottom' => '
    some text
    badElement
  '
        };






More information about the Xml-compile mailing list