[studxml-users] namespace, prefix & xsi attr

Boris Kolpackov boris at codesynthesis.com
Mon Feb 23 08:21:32 EST 2015


Maxim Maslennikov <maxim.maslennikov at gmail.com> writes:

> Could you explain how to parse and serialize following root element:
>
> <t:root xmlns:t='test'
>         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
>         xsi:schemaLocation='test test.xsd'>
> 
> I’m able to parse:
> 
> p.next_expect(parser::start_element, "test", "root", content::complex);
> string namespace_ = p.namespace_();
> string name = p.name();
> string prefix = p.prefix();

>From <xml/parser>:

const std::string&
attribute (const qname_type& qname) const;

template <typename T>
T
attribute (const qname_type& qname) const;

std::string
attribute (const qname_type& qname,
           const std::string& default_value) const;

template <typename T>
T
attribute (const qname_type& qname, const T& default_value) const;

bool
attribute_present (const qname_type& qname) const;

So:

string schema_location =
  p.attribute (qname ("http://www.w3.org/2001/XMLSchema-instance",
                      "schemaLocation"));

> And serialize:
> s.start_element( namespace_, name );
> s.namespace_decl( namespace, prefix );

>From <xml/serializer>:

void
attribute (const qname_type& qname, const std::string& value);

template <typename T>
void
attribute (const qname_type& qname, const T& value);

void
attribute (const std::string& ns,
           const std::string& name,
           const std::string& value);

template <typename T>
void
attribute (const std::string& ns,
           const std::string& name,
           const T& value);

And:

// Namespaces declaration. If prefix is empty, then the default
// namespace is declared. If both prefix and namespace are empty,
// then the default namespace declaration is cleared (xmlns="").
//
void
namespace_decl (const std::string& ns, const std::string& prefix);

So:

s.namespace_decl ("http://www.w3.org/2001/XMLSchema-instance", "xsi");
s.attribute ("http://www.w3.org/2001/XMLSchema-instance",
             "schemaLocation",
	     "test test.xsd");

Moral of the story: don't be afraid to look into the API headers, it's
all in there.

Boris



More information about the studxml-users mailing list