More work on Key support:
authorDaniel Veillard <veillard@src.gnome.org>
Wed, 7 Feb 2001 21:34:20 +0000 (21:34 +0000)
committerDaniel Veillard <veillard@src.gnome.org>
Wed, 7 Feb 2001 21:34:20 +0000 (21:34 +0000)
- libxslt/functions.c FEATURES: started adding support for key()
- tests/REC/test-12.2-1.*: first key test
Daniel

ChangeLog
FEATURES
libxslt/functions.c
tests/REC/test-12.2-1.out [new file with mode: 0644]
tests/REC/test-12.2-1.xml [new file with mode: 0644]
tests/REC/test-12.2-1.xsl [new file with mode: 0644]

index 1f511c9..683dcb4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Feb  7 21:16:47 CET 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
+
+       * libxslt/functions.c FEATURES: started adding support for key()
+       * tests/REC/test-12.2-1.*: first key test
+
 Wed Feb  7 19:46:07 CET 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
 
        * FEATURES: updated
index e36052c..c7556d0 100644 (file)
--- a/FEATURES
+++ b/FEATURES
@@ -208,7 +208,7 @@ Functions:
 ==========
 
 PARTIAL                            node-set document(object, node-set?)
-NO                         node-set key(string, object)
+PARTIAL                            node-set key(string, object)
 YES                        string format-number(number, string, string?)
 YES                        node-set current() 
 YES                        string unparsed-entity-uri(string)
index dab6bc7..29d8b57 100644 (file)
@@ -36,6 +36,7 @@
 #include "xsltutils.h"
 #include "functions.h"
 #include "numbersInternals.h"
+#include "keys.h"
 
 #define DEBUG_FUNCTION
 
@@ -142,7 +143,64 @@ xsltDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs){
  */
 void
 xsltKeyFunction(xmlXPathParserContextPtr ctxt, int nargs){
-    TODO /* function */
+    xmlNodeSetPtr nodelist;
+    xmlXPathObjectPtr obj, tmp;
+    xmlChar *key, *value;
+    xsltTransformContextPtr tctxt;
+
+    tctxt = ((xsltTransformContextPtr)ctxt->context->extra);
+
+    if (nargs != 2) {
+        xsltGenericError(xsltGenericErrorContext,
+               "key() : expects one string arg\n");
+       ctxt->error = XPATH_INVALID_ARITY;
+       return;
+    }
+
+    obj = valuePop(ctxt);
+    if ((obj == NULL) ||
+       (ctxt->value == NULL) || (ctxt->value->type != XPATH_STRING)) {
+       xsltGenericError(xsltGenericErrorContext,
+           "generate-id() : invalid arg expecting a string\n");
+       ctxt->error = XPATH_INVALID_TYPE;
+       xmlXPathFreeObject(obj);
+
+       return;
+    }
+    tmp = valuePop(ctxt);
+    key = tmp->stringval;
+
+    /* TODO: find URI when qualified name */
+    
+    if (obj->type == XPATH_NODESET) {
+       TODO /* handle NODE set as 2nd args of key() */
+    } else {
+       /*
+        * Force conversion of first arg to string
+        */
+       valuePush(ctxt, obj);
+       xmlXPathStringFunction(ctxt, 1);
+       if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_STRING)) {
+           xsltGenericError(xsltGenericErrorContext,
+               "generate-id() : invalid arg expecting a string\n");
+           ctxt->error = XPATH_INVALID_TYPE;
+           xmlXPathFreeObject(obj);
+
+           return;
+       }
+       obj = valuePop(ctxt);
+       value = obj->stringval;
+
+       nodelist = xsltGetKey(tctxt, key, NULL, value);
+       valuePush(ctxt, xmlXPathWrapNodeSet(
+                       xmlXPathNodeSetMerge(NULL, nodelist)));
+    }
+
+
+    if (obj != NULL)
+       xmlXPathFreeObject(obj);
+    if (tmp != NULL)
+       xmlXPathFreeObject(tmp);
 }
 
 /**
diff --git a/tests/REC/test-12.2-1.out b/tests/REC/test-12.2-1.out
new file mode 100644 (file)
index 0000000..77c975e
--- /dev/null
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+Success
diff --git a/tests/REC/test-12.2-1.xml b/tests/REC/test-12.2-1.xml
new file mode 100644 (file)
index 0000000..b61bdaa
--- /dev/null
@@ -0,0 +1,4 @@
+<doc>
+  <div id="lookup">Success</div>
+  <div id="unwanted">Failed</div>
+</doc>
diff --git a/tests/REC/test-12.2-1.xsl b/tests/REC/test-12.2-1.xsl
new file mode 100644 (file)
index 0000000..daf297a
--- /dev/null
@@ -0,0 +1,11 @@
+<xsl:stylesheet version="1.0"
+      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+      xmlns:fo="http://www.w3.org/1999/XSL/Format">
+
+<xsl:key name="idkey" match="div" use="@id"/>
+
+<xsl:template match="doc">
+<xsl:value-of select="key('idkey','lookup')"/>
+</xsl:template>
+
+</xsl:stylesheet>