OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLXmlHelpers.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLXMLHELPERS_HPP
12#define OPENXLSX_XLXMLHELPERS_HPP
13
24
25#include <initializer_list>
26#include <string>
27#include <string_view>
28#include <type_traits>
29#include <vector>
30
31#include "XLXmlParser.hpp"
32
33namespace OpenXLSX
34{
35 // =========================================================================
36 // Flags shared by setChildAttr / appendAndSetNodeAttribute
37 // =========================================================================
38
39 constexpr bool XLRemoveAttributes = true;
40 constexpr bool XLKeepAttributes = false;
41
42 constexpr int SORT_INDEX_NOT_FOUND = -1;
43
47 inline int findStringInVector(std::string_view nodeName, std::vector<std::string_view> const& nodeOrder)
48 {
49 for (size_t i = 0; i < nodeOrder.size(); ++i)
50 if (nodeName == nodeOrder[i]) return static_cast<int>(i);
52 }
53
57 inline void copyLeadingWhitespaces(XMLNode& parent, XMLNode fromNode, XMLNode toNode)
58 {
59 fromNode = fromNode.previous_sibling();
60 while (fromNode.type() == node_pcdata) {
61 toNode = parent.insert_child_before(node_pcdata, toNode);
62 toNode.set_value(fromNode.value());
63 fromNode = fromNode.previous_sibling();
64 }
65 }
66
67 // =========================================================================
68 // ensureChild — get-or-create child element (optional MS Office node order)
69 // =========================================================================
70
76 inline XMLNode ensureChild(XMLNode& parent,
77 const char* nodeName,
78 std::vector<std::string_view> const& nodeOrder = {},
79 bool force_ns = false)
80 {
81 if (parent.empty() || nodeName == nullptr || *nodeName == '\0') return XMLNode{};
82
83 XMLNode nextNode = parent.first_child_of_type(node_element);
84 if (nextNode.empty()) return parent.prepend_child(nodeName, force_ns);
85
86 XMLNode node{};
87
88 const int nodeSortIndex = (nodeOrder.size() > 1 ? findStringInVector(nodeName, nodeOrder) : SORT_INDEX_NOT_FOUND);
89 if (nodeSortIndex != SORT_INDEX_NOT_FOUND) {
90 while (not nextNode.empty() and findStringInVector(nextNode.raw_name(), nodeOrder) < nodeSortIndex)
91 nextNode = nextNode.next_sibling_of_type(node_element);
92
93 if (not nextNode.empty()) {
94 if (std::string_view(nextNode.raw_name()) == std::string_view(nodeName))
95 node = nextNode;
96 else {
97 node = parent.insert_child_before(nodeName, nextNode, force_ns);
98 copyLeadingWhitespaces(parent, node, nextNode);
99 }
100 }
101 }
102 else
103 node = parent.child(nodeName);
104
105 if (node.empty()) {
106 nextNode = parent.last_child_of_type(node_element);
107 node = parent.insert_child_after(nodeName, nextNode, force_ns);
108 copyLeadingWhitespaces(parent, nextNode, node);
109 }
110
111 return node;
112 }
113
115 std::string const& nodeName,
116 std::vector<std::string_view> const& nodeOrder = {},
117 bool force_ns = false)
118 {
119 return ensureChild(parent, nodeName.c_str(), nodeOrder, force_ns);
120 }
121
126 inline XMLNode ensureChildPath(XMLNode& parent, std::initializer_list<const char*> path)
127 {
128 XMLNode current = parent;
129 for (const char* name : path) {
130 if (current.empty()) return XMLNode{};
131 current = ensureChild(current, name);
132 }
133 return current;
134 }
135
136 // =========================================================================
137 // ensureAttr / setAttr — attribute get-or-create / always-set
138 // =========================================================================
139
143 inline XMLAttribute ensureAttr(XMLNode& node, const char* attrName, const char* defaultVal = "")
144 {
145 if (node.empty() || attrName == nullptr || *attrName == '\0') return XMLAttribute{};
146 XMLAttribute attr = node.attribute(attrName);
147 if (attr.empty()) {
148 attr = node.append_attribute(attrName);
149 attr.set_value(defaultVal != nullptr ? defaultVal : "");
150 }
151 return attr;
152 }
153
154 inline XMLAttribute ensureAttr(XMLNode& node, std::string const& attrName, std::string const& defaultVal)
155 {
156 return ensureAttr(node, attrName.c_str(), defaultVal.c_str());
157 }
158
162 inline XMLAttribute setAttr(XMLNode& node, const char* attrName, const char* attrVal)
163 {
164 if (node.empty() || attrName == nullptr || *attrName == '\0') return XMLAttribute{};
165 XMLAttribute attr = node.attribute(attrName);
166 if (attr.empty()) attr = node.append_attribute(attrName);
167 attr.set_value(attrVal != nullptr ? attrVal : "");
168 return attr;
169 }
170
171 inline XMLAttribute setAttr(XMLNode& node, const char* attrName, std::string_view attrVal)
172 {
173 if (node.empty() || attrName == nullptr || *attrName == '\0') return XMLAttribute{};
174 XMLAttribute attr = node.attribute(attrName);
175 if (attr.empty()) attr = node.append_attribute(attrName);
176 const std::string tmp(attrVal);
177 attr.set_value(tmp.c_str());
178 return attr;
179 }
180
181 inline XMLAttribute setAttr(XMLNode& node, const char* attrName, std::string const& attrVal)
182 {
183 return setAttr(node, attrName, attrVal.c_str());
184 }
185
186 inline XMLAttribute setAttr(XMLNode& node, std::string const& attrName, std::string const& attrVal)
187 {
188 return setAttr(node, attrName.c_str(), attrVal.c_str());
189 }
190
195 template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
196 inline XMLAttribute setAttr(XMLNode& node, const char* attrName, T attrVal)
197 {
198 if (node.empty() || attrName == nullptr || *attrName == '\0') return XMLAttribute{};
199 XMLAttribute attr = node.attribute(attrName);
200 if (attr.empty()) attr = node.append_attribute(attrName);
201 attr.set_value(attrVal);
202 return attr;
203 }
204
206 inline XMLAttribute setAttrBool01(XMLNode& node, const char* attrName, bool value)
207 {
208 return setAttr(node, attrName, value ? "1" : "0");
209 }
210
212 inline XMLAttribute setAttrBoolTF(XMLNode& node, const char* attrName, bool value)
213 {
214 return setAttr(node, attrName, value ? "true" : "false");
215 }
216
217 // =========================================================================
218 // setChildAttr / ensureChildAttr — child element + attribute
219 // =========================================================================
220
225 const char* nodeName,
226 const char* attrName,
227 const char* attrDefaultVal,
228 std::vector<std::string_view> const& nodeOrder = {})
229 {
230 if (parent.empty()) return XMLAttribute{};
231 XMLNode node = ensureChild(parent, nodeName, nodeOrder);
232 return ensureAttr(node, attrName, attrDefaultVal);
233 }
234
236 std::string const& nodeName,
237 std::string const& attrName,
238 std::string const& attrDefaultVal,
239 std::vector<std::string_view> const& nodeOrder = {})
240 {
241 return ensureChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrDefaultVal.c_str(), nodeOrder);
242 }
243
248 const char* nodeName,
249 const char* attrName,
250 const char* attrVal,
251 bool removeAttributes = XLKeepAttributes,
252 std::vector<std::string_view> const& nodeOrder = {})
253 {
254 if (parent.empty()) return XMLAttribute{};
255 XMLNode node = ensureChild(parent, nodeName, nodeOrder);
256 if (removeAttributes) node.remove_attributes();
257 return setAttr(node, attrName, attrVal);
258 }
259
261 std::string const& nodeName,
262 std::string const& attrName,
263 std::string const& attrVal,
264 bool removeAttributes = XLKeepAttributes,
265 std::vector<std::string_view> const& nodeOrder = {})
266 {
267 return setChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrVal.c_str(), removeAttributes, nodeOrder);
268 }
269
270 // =========================================================================
271 // val-attribute & text helpers (spreadsheetml / chartml common patterns)
272 // =========================================================================
273
275 inline XMLAttribute setVal(XMLNode& node, const char* value) { return setAttr(node, "val", value); }
276
277 inline XMLAttribute setVal(XMLNode& node, std::string_view value) { return setAttr(node, "val", value); }
278
283 const char* childName,
284 const char* value,
285 std::vector<std::string_view> const& nodeOrder = {})
286 {
287 return setChildAttr(parent, childName, "val", value, XLKeepAttributes, nodeOrder);
288 }
289
291 const char* childName,
292 std::string_view value,
293 std::vector<std::string_view> const& nodeOrder = {})
294 {
295 const std::string tmp(value);
296 return setChildVal(parent, childName, tmp.c_str(), nodeOrder);
297 }
298
303 const char* childName,
304 std::string_view text,
305 std::vector<std::string_view> const& nodeOrder = {})
306 {
307 XMLNode node = ensureChild(parent, childName, nodeOrder);
308 if (not node.empty()) {
309 const std::string tmp(text);
310 node.text().set(tmp.c_str());
311 }
312 return node;
313 }
314
319 inline bool getBoolAttributeWhenOmittedMeansTrue(XMLNode& parent, const char* tagName, const char* attrName = "val")
320 {
321 if (parent.empty() || tagName == nullptr) return false;
322 XMLNode tagNode = parent.child(tagName);
323 if (tagNode.empty()) return false;
324 XMLAttribute valAttr = tagNode.attribute(attrName);
325 if (valAttr.empty()) {
326 setAttr(tagNode, attrName, "true");
327 return true;
328 }
329 return valAttr.as_bool();
330 }
331
332 inline bool getBoolAttributeWhenOmittedMeansTrue(XMLNode& parent, std::string const& tagName, std::string const& attrName = "val")
333 {
334 return getBoolAttributeWhenOmittedMeansTrue(parent, tagName.c_str(), attrName.c_str());
335 }
336
337 // =========================================================================
338 // Legacy names (appendAnd*) — thin aliases for source compatibility
339 // =========================================================================
340
342 std::string const& nodeName,
343 std::vector<std::string_view> const& nodeOrder = {},
344 bool force_ns = false)
345 {
346 return ensureChild(parent, nodeName.c_str(), nodeOrder, force_ns);
347 }
348
349 inline XMLAttribute appendAndGetAttribute(XMLNode& node, std::string const& attrName, std::string const& attrDefaultVal)
350 {
351 return ensureAttr(node, attrName.c_str(), attrDefaultVal.c_str());
352 }
353
354 inline XMLAttribute appendAndSetAttribute(XMLNode& node, std::string const& attrName, std::string const& attrVal)
355 {
356 return setAttr(node, attrName.c_str(), attrVal.c_str());
357 }
358
360 std::string const& nodeName,
361 std::string const& attrName,
362 std::string const& attrDefaultVal,
363 std::vector<std::string_view> const& nodeOrder = {})
364 {
365 return ensureChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrDefaultVal.c_str(), nodeOrder);
366 }
367
369 std::string const& nodeName,
370 std::string const& attrName,
371 std::string const& attrVal,
372 bool removeAttributes = XLKeepAttributes,
373 std::vector<std::string_view> const& nodeOrder = {})
374 {
375 return setChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrVal.c_str(), removeAttributes, nodeOrder);
376 }
377
378} // namespace OpenXLSX
379
380#endif // OPENXLSX_XLXMLHELPERS_HPP
XMLNode previous_sibling(const char *name_) const
Definition XLXmlParser.cpp:168
XMLAttribute append_attribute(const char *name_)
Definition XLXmlParser.cpp:114
XMLNode prepend_child(T type_=node_element)
Definition XLXmlParser.hpp:164
XMLNode child(const char *name_) const
Definition XLXmlParser.cpp:150
XMLNode last_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:208
bool set_value(const char *rhs)
Definition XLXmlParser.cpp:90
XMLAttribute attribute(const char *name_) const
Definition XLXmlParser.cpp:96
XMLNode insert_child_before(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:176
XMLNode first_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:204
bool empty() const
Definition XLXmlParser.cpp:58
XMLNodeType type() const
Definition XLXmlParser.cpp:283
const char * value() const
Definition XLXmlParser.cpp:72
XMLNode insert_child_after(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:172
Definition XLXmlParser.hpp:283
bool set_value(const char *rhs)
Definition XLXmlParser.cpp:533
bool as_bool(bool def=false) const
Definition XLXmlParser.cpp:617
bool empty() const
Definition XLXmlParser.cpp:513
Definition IXLCellProvider.hpp:24
XMLAttribute setAttrBoolTF(XMLNode &node, const char *attrName, bool value)
Boolean attribute as "true" / "false".
Definition XLXmlHelpers.hpp:212
void copyLeadingWhitespaces(XMLNode &parent, XMLNode fromNode, XMLNode toNode)
Copy leading pcdata (whitespace) siblings from fromNode before toNode.
Definition XLXmlHelpers.hpp:57
XMLAttribute appendAndSetNodeAttribute(XMLNode &parent, std::string const &nodeName, std::string const &attrName, std::string const &attrVal, bool removeAttributes=XLKeepAttributes, std::vector< std::string_view > const &nodeOrder={})
Definition XLXmlHelpers.hpp:368
XMLAttribute ensureChildAttr(XMLNode &parent, const char *nodeName, const char *attrName, const char *attrDefaultVal, std::vector< std::string_view > const &nodeOrder={})
Ensure child nodeName exists and return its attribute attrName (create with default).
Definition XLXmlHelpers.hpp:224
XMLNode setChildText(XMLNode &parent, const char *childName, std::string_view text, std::vector< std::string_view > const &nodeOrder={})
Ensure child exists (optional order) and set its text content.
Definition XLXmlHelpers.hpp:302
bool getBoolAttributeWhenOmittedMeansTrue(XMLNode &parent, const char *tagName, const char *attrName="val")
Bool tag with omitted @val meaning true (legacy OOXML quirk).
Definition XLXmlHelpers.hpp:319
XMLNode ensureChildPath(XMLNode &parent, std::initializer_list< const char * > path)
Ensure a chain of simple (unordered) child elements exists.
Definition XLXmlHelpers.hpp:126
constexpr XMLNodeType node_element
Definition XLXmlParser.hpp:71
XMLAttribute ensureAttr(XMLNode &node, const char *attrName, const char *defaultVal="")
Ensure attribute exists; write defaultVal only when creating.
Definition XLXmlHelpers.hpp:143
XMLAttribute appendAndGetAttribute(XMLNode &node, std::string const &attrName, std::string const &attrDefaultVal)
Definition XLXmlHelpers.hpp:349
XMLAttribute setAttr(XMLNode &node, const char *attrName, const char *attrVal)
Ensure attribute exists and always set its value (string).
Definition XLXmlHelpers.hpp:162
XMLNode ensureChild(XMLNode &parent, const char *nodeName, std::vector< std::string_view > const &nodeOrder={}, bool force_ns=false)
Ensure a child element named nodeName exists under parent and return it.
Definition XLXmlHelpers.hpp:76
XMLAttribute setAttrBool01(XMLNode &node, const char *attrName, bool value)
Excel-style boolean attribute as "1" / "0".
Definition XLXmlHelpers.hpp:206
XMLAttribute appendAndGetNodeAttribute(XMLNode &parent, std::string const &nodeName, std::string const &attrName, std::string const &attrDefaultVal, std::vector< std::string_view > const &nodeOrder={})
Definition XLXmlHelpers.hpp:359
XMLAttribute setChildVal(XMLNode &parent, const char *childName, const char *value, std::vector< std::string_view > const &nodeOrder={})
Ensure child childName exists under parent and set its @val attribute.
Definition XLXmlHelpers.hpp:282
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
constexpr bool XLRemoveAttributes
Definition XLXmlHelpers.hpp:39
XMLAttribute appendAndSetAttribute(XMLNode &node, std::string const &attrName, std::string const &attrVal)
Definition XLXmlHelpers.hpp:354
constexpr int SORT_INDEX_NOT_FOUND
Definition XLXmlHelpers.hpp:42
constexpr bool XLKeepAttributes
Definition XLXmlHelpers.hpp:40
constexpr XMLNodeType node_pcdata
Definition XLXmlParser.hpp:72
XMLAttribute setVal(XMLNode &node, const char *value)
Set the @val attribute on node.
Definition XLXmlHelpers.hpp:275
XMLNode appendAndGetNode(XMLNode &parent, std::string const &nodeName, std::vector< std::string_view > const &nodeOrder={}, bool force_ns=false)
Definition XLXmlHelpers.hpp:341
int findStringInVector(std::string_view nodeName, std::vector< std::string_view > const &nodeOrder)
Index of nodeName in nodeOrder, or SORT_INDEX_NOT_FOUND.
Definition XLXmlHelpers.hpp:47
XMLAttribute setChildAttr(XMLNode &parent, const char *nodeName, const char *attrName, const char *attrVal, bool removeAttributes=XLKeepAttributes, std::vector< std::string_view > const &nodeOrder={})
Ensure child exists, optionally clear its attributes, then set attrName.
Definition XLXmlHelpers.hpp:247