Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread

 

By: Manuel Binna
RE: SaxParser: Access attribute prefixes [ reply ]  
2010-08-16 11:52
I just found, that the underlying function start_element_ns_callback() in ruby_xml_sax2_handler.c processes the attributes as follows:

// ...

/* Each attribute is an array of [localname, prefix, URI, value, end] */
int i;
for (i = 0;i < nb_attributes * 5; i+=5)
{
VALUE attrName = rb_str_new2(xattributes[i+0]);
VALUE attrValue = rb_str_new(xattributes[i+3], xattributes[i+4] - xattributes[i+3]);
/* VALUE attrPrefix = xattributes[i+1] ? rb_str_new2(xattributes[i+1]) : Qnil;
VALUE attrURI = xattributes[i+2] ? rb_str_new2(xattributes[i+2]) : Qnil; */

rb_hash_aset(attributes, attrName, attrValue);
}

// ...

Because the prefix and the URI are commented out, it is logical that libxml-ruby does not provide them to the implementation of on_start_element_ns() callback. But why are they commented out?

By: Manuel Binna
SaxParser: Access attribute prefixes [ reply ]  
2010-08-16 10:21
Is it possible to access the namespace prefixes on attributes when using LibXML::XML::SaxParser and LibXML::XML::SaxParser::Callbacks? The callback on_start_element_ns (name, attributes, prefix, uri, namespaces) does provides a Hash 'attributes' with the attribute's local name and value, but I didn't manage to access the attribute's namespace prefixes defined in my input XML.

Note: libxml2's C-API provides the callback xmlSAX2StartElementNs() which provides a "pointer to the array of (localname/prefix/URI/value/end) attribute values". So accessing the attribute's prefix in pure libxml2 seems to be possible.