Xport Interface: markup::attribute
For a derived element
object, sets or retrieves an attribute value.
virtual std::string markup::attribute(1st Variety
xhtml_attribute attrib
);
virtual bool markup::attribute(2nd Variety
xhtml_attribute attrib,
const std::string& value
);
Parameters
attrib
An xhtml_attribute
enumerator. For a list of xhtml_attribute
enumerators, see Xport Usage: Enumerations.
value
A string value which represents the value of the accompanied attribute.
Returns
In the first variety, returns the string value of the specified attribute if called on an element object and the attribute is present within the
element, or an empty string otherwise. In the second variety, returns true
if called on an element object and the attribute is set
successfully, false
otherwise.
Remarks
This operation is revelant only if it is called on a derived element
object. If called on a pcdata
, comment
,
or procinstr
object, the first variety will always return an empty string, and the second variety will always return false.
When called on a derived element
object, the operation will be executed appropriately. See element::attribute() for more details.
Complexity
Constant
Example
1st Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
document doc(root_doc);
doc.body()->insert(comment("comment in body element"));
doc.body()->insert(element(p, "top_par") << "Top paragraph.");
markup::const_descendant_iterator it = doc.body()->descendant_begin();
for (; it != doc.body()->descendant_end(); ++it) {
std::string attr_val = it->attribute(id);
xhtml_markup_type mt = it->markup_type();
std::cout << (mt == mt_element ? "element" : mt == mt_comment ? "comment" : "pcdata");
if (mt == mt_element) {
std::cout << " " << it->tag_name();
}
if (!attr_val.empty()) {
std::cout << " has id attribute '" << attr_val << "'.\n";
} else {
std::cout << " does not have an id attribute.\n";
}
}
return 0;
}
2nd Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
document doc(root_doc);
markup::iterator it = doc.body()->insert(comment("comment in body element"));
bool success = it->attribute(attribute::title, "comment1");
std::cout << (success ? "Set" : "Could not set") << " comment's name attribute.\n";
it = doc.body()->insert(element(p));
success = it->attribute(attribute::title, "paragraph1");
std::cout << (success ? "Set" : "Could not set") << " paragraph element's name attribute.\n";
it = it->insert("All PCDATA is stored in a pcdata object");
success = it->attribute(attribute::title, "pcdata1");
std::cout << (success ? "Set" : "Could not set") << " pcdata's name attribute.\n";
return 0;
}