[PATCH 3/3] parse-xml: allow XML nodes with empty tag names

Linus Torvalds torvalds at linux-foundation.org
Mon Jan 28 21:22:45 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Mon, 28 Jan 2013 21:12:32 -0800
Subject: [PATCH 3/3] parse-xml: allow XML nodes with empty tag names

They happen for CDATA content, where libxml2 turns the CDATA fields into
a child of the parent entry, but without a name.

Now, of course, any sane person would just want to use the CDATA as the
string value of the parent itself, but libxml2 probably does this
insanity for a reason.  And the reason is probably that some misguided
people want to *write* XML using libxml2, and then the stupid child node
actually acts as a "now I want you to write this data as CDATA".

Whatever the reason, let's just ignore it.  We will just traverse such a
nameless child and be happy, and we'll give the nameless child the name
of the parent.  Our XML node matching logic will then never see this
insane nameless child at all, and doesn't have to care.

Our whole XML parsing rule-of-thumb is to take the whole "be strict in
what you output, but generous in what you accept" to its logical
conclusion.  Because we will literally accept almost anything, in any
format.  You can mix tags or attributes wildly, and youc an use CDATA or
not as you see fit.  We just don't care.

We're the honeybadger of the divelog world.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 parse-xml.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/parse-xml.c b/parse-xml.c
index 765843fbc56e..30b616324b00 100644
--- a/parse-xml.c
+++ b/parse-xml.c
@@ -1350,7 +1350,7 @@ static void visit_one_node(xmlNode *node)
 		return;
 
 	/* Don't print out the node name if it is "text" */
-	if (!strcmp(node->name, "text"))
+	while (!node->name || !strcmp(node->name, "text"))
 		node = node->parent;
 
 	name = nodename(node, buffer, sizeof(buffer));
@@ -1438,6 +1438,11 @@ static void traverse(xmlNode *root)
 	for (n = root; n; n = n->next) {
 		struct nesting *rule = nesting;
 
+		if (!n->name) {
+			visit(n);
+			continue;
+		}
+
 		do {
 			if (!strcmp(rule->name, n->name))
 				break;
-- 
1.8.1.2.422.g08c0e7f



More information about the subsurface mailing list