shithub: xmltools

Download patch

ref: fc012e4dcc216d33f07262e64e3417043a7aa564
parent: dd05e41b9a71be3441f36d9b944274da3cd64f35
author: sirjofri <sirjofri@sirjofri.de>
date: Wed Jul 24 11:18:17 EDT 2024

updates xq.c to work with new xpath version, adds WIP xslt which uses namespace features

--- a/README
+++ b/README
@@ -1,6 +1,6 @@
 XML tools
 
-This package requires libxml from 9atom and libxpath.
+This package requires the updated libxml from 9atom and libxpath.
 See also:
 - https://shithub.us/sirjofri/xml-9atom/HEAD/info.html
 - https://git.sr.ht/~sirjofri/xml-9atom
--- a/mkfile
+++ b/mkfile
@@ -1,8 +1,8 @@
 </$objtype/mkfile
 
 BIN=/$objtype/bin
-TARG=xq
-OFILES=\
-	xq.$O\
+TARG=\
+	xq\
+	xslt\
 
 </sys/src/cmd/mkmany
--- a/xq.c
+++ b/xq.c
@@ -204,6 +204,12 @@
 	free(t);
 }
 
+static void
+printnum(int num)
+{
+	print("%d\n", num);
+}
+
 void
 query2(char *q, Xml *x)
 {
@@ -218,13 +224,17 @@
 	default:
 		fprint(2, "not found\n");
 		break;
-	case XTstring:
+	case Xstring:
 		for (i = 0; i < r.num; i++)
 			printstr(r.strings[i]);
 		break;
-	case XTelem:
+	case Xelem:
 		for (i = 0; i < r.num; i++)
 			printelem(r.elems[i]);
+		break;
+	case Xnum:
+		for (i = 0; i < r.num; i++)
+			printnum(r.numbers[i]);
 		break;
 	}
 }
--- /dev/null
+++ b/xslt.c
@@ -1,0 +1,60 @@
+#include <u.h>
+#include <libc.h>
+#include <xml.h>
+#include <xpath.h>
+
+Xml *xml = nil;
+Xml *style = nil;
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s\n", argv0);
+	exits(nil);
+}
+
+void
+main(int argc, char **argv)
+{
+	int fd;
+	char *s = nil;
+
+	ARGBEGIN{
+	case 'h':
+		usage();
+		break;
+	case 's':
+		s = EARGF(usage());
+		break;
+	}ARGEND;
+
+	if (argc && *argv[0]) {
+		fd = open(argv[0], OREAD);
+		if (fd < 0)
+			sysfatal("unable to open file: %r");
+		xml = xmlparse(fd, 8192, 0);
+		close(fd);
+	} else {
+		xml = xmlparse(0, 8192, 0);
+	}
+
+	xmldebug = 1;
+	if (s) {
+		fd = open(s, OREAD);
+		if (fd < 0)
+			sysfatal("unable to open file: %r");
+		style = xmlparse(fd, 8192, 0);
+		close(fd);
+	}
+	
+	if (!style)
+		sysfatal("err: %r");
+	
+	xmlprint(style, 2);
+	
+	Ns *ns;
+	
+	for (ns = style->ns; ns; ns = ns->next) {
+		fprint(2, "ns: %s → %s\n", ns->name, ns->decl);
+	}
+}