1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <orcus/xml_namespace.hpp>
#include <iostream>
using namespace orcus;
using namespace std;
void run_xmlns_example()
{
// A namespace repository is a shared storage for namespace strings for
// multiple contexts. The client code needs to simply create an instance
// from which to create contexts.
xmlns_repository ns_repo;
xmlns_context ns_cxt = ns_repo.create_context();
// Push namespaces with their aliases as you counter them. The push()
// method then returns an identifier associated with the alias.
// empty alias is for default namespace. You can either use nullptr or an
// empty string.
xmlns_id_t ns_default = ns_cxt.push(
std::string_view{}, "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
xmlns_id_t ns_a = ns_cxt.push(
"a", "http://schemas.openxmlformats.org/drawingml/2006/main");
xmlns_id_t ns_r = ns_cxt.push(
"r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
// You can retrieve the data associated with alias ID's.
for (const xmlns_id_t nsid : {ns_default, ns_a, ns_r})
{
std::string_view alias = ns_cxt.get_alias(nsid);
cout << "Namespace alias '" << alias << "' has an index of " << ns_cxt.get_index(nsid)
<< " and a short name of '" << ns_cxt.get_short_name(nsid) << "'." << endl;
cout << "The value of the alias '" << alias << "' is '" << ns_cxt.get(alias) << "'." << endl;
}
// Iterate over all namespaces in the current context.
for (const xmlns_id_t nsid : ns_cxt.get_all_namespaces())
cout << "'" << ns_cxt.get_alias(nsid) << "' = " << ns_cxt.get_short_name(nsid) << endl;
}
void run_xmlns_stacked()
{
xmlns_repository ns_repo;
xmlns_context ns_cxt = ns_repo.create_context();
// Push a first default namespace.
xmlns_id_t ns_default_1 = ns_cxt.push(std::string_view{}, "http://original");
// Push a nested deffault namespace. This overwrites the original.
xmlns_id_t current_default_ns = ns_cxt.push(std::string_view{}, "http://nested");
cout << "same as original: " << (current_default_ns == ns_default_1) << endl;
// Pop the current default namespace. After this the original namespace
// becomes the default namespace again.
ns_cxt.pop(std::string_view{});
// Get the current default namespace identifier.
current_default_ns = ns_cxt.get(std::string_view{});
cout << "same as original: " << (current_default_ns == ns_default_1) << endl;
}
void run_xmlns_same_ns_different_aliases()
{
xmlns_repository ns_repo;
// Same namespace URI may be associated with different aliases in different
// contexts.
xmlns_id_t alias_1, alias_2;
{
xmlns_context ns_cxt = ns_repo.create_context();
alias_1 = ns_cxt.push("foo", "http://some-namespace");
for (const xmlns_id_t nsid : ns_cxt.get_all_namespaces())
cout << "'" << ns_cxt.get_alias(nsid) << "' = " << ns_cxt.get_short_name(nsid) << endl;
}
{
xmlns_context ns_cxt = ns_repo.create_context();
alias_2 = ns_cxt.push("bar", "http://some-namespace");
for (const xmlns_id_t nsid : ns_cxt.get_all_namespaces())
cout << "'" << ns_cxt.get_alias(nsid) << "' = " << ns_cxt.get_short_name(nsid) << endl;
}
cout << (alias_1 == alias_2 ? "same" : "different") << endl;
}
void run_xmlns_different_ns_same_alias()
{
xmlns_repository ns_repo;
// Same alias may be associated with different namespace URI's in different
// contexts.
xmlns_id_t alias_1, alias_2;
{
xmlns_context ns_cxt = ns_repo.create_context();
alias_1 = ns_cxt.push("foo", "http://namespace-1");
for (const xmlns_id_t nsid : ns_cxt.get_all_namespaces())
cout << "'" << ns_cxt.get_alias(nsid) << "' = " << ns_cxt.get_short_name(nsid) << endl;
}
{
xmlns_context ns_cxt = ns_repo.create_context();
alias_2 = ns_cxt.push("foo", "http://namespace-2");
for (const xmlns_id_t nsid : ns_cxt.get_all_namespaces())
cout << "'" << ns_cxt.get_alias(nsid) << "' = " << ns_cxt.get_short_name(nsid) << endl;
}
cout << (alias_1 == alias_2 ? "same" : "different") << endl;
}
int main()
{
run_xmlns_example();
run_xmlns_stacked();
run_xmlns_same_ns_different_aliases();
run_xmlns_different_ns_same_alias();
return EXIT_SUCCESS;
}
|