Bob Stayton pointed out a problem when using unions in key match patterns.
authorDaniel Veillard <veillard@src.gnome.org>
Wed, 30 Jan 2002 11:39:41 +0000 (11:39 +0000)
committerDaniel Veillard <veillard@src.gnome.org>
Wed, 30 Jan 2002 11:39:41 +0000 (11:39 +0000)
* libxslt/keys.c: Bob Stayton pointed out a problem when
  using unions in key match patterns.
* tests/docs/Makefile.am tests/docs/bug-70.xml
  tests/general/Makefile.am tests/general/bug-70.*: added a
  specific example in the regression tests
Daniel

ChangeLog
libxslt/keys.c
libxslt/xslt.c
tests/docs/Makefile.am
tests/docs/bug-70.xml [new file with mode: 0644]
tests/general/Makefile.am
tests/general/bug-70.out [new file with mode: 0644]
tests/general/bug-70.xsl [new file with mode: 0644]

index d09a8f8..4be8214 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Jan 30 12:35:28 CET 2002 Daniel Veillard <daniel@veillard.com>
+
+       * libxslt/keys.c: Bob Stayton pointed out a problem when
+         using unions in key match patterns.
+       * tests/docs/Makefile.am tests/docs/bug-70.xml
+         tests/general/Makefile.am tests/general/bug-70.*: added a 
+         specific example in the regression tests
+       
 Sun Jan 27 13:54:10 CET 2002 Daniel Veillard <daniel@veillard.com>
 
        * libxslt/transform.c: avoid a problem with Sun's Workshop CC,
index f3a7a20..47d31dd 100644 (file)
@@ -236,6 +236,7 @@ xsltAddKey(xsltStylesheetPtr style, const xmlChar *name,
           const xmlChar *use, xmlNodePtr inst) {
     xsltKeyDefPtr key;
     xmlChar *pattern = NULL;
+    int current, end, start;
 
     if ((style == NULL) || (name == NULL) || (match == NULL) || (use == NULL))
        return(-1);
@@ -250,12 +251,48 @@ xsltAddKey(xsltStylesheetPtr style, const xmlChar *name,
     key->use = xmlStrdup(use);
     key->inst = inst;
 
-    if (key->match[0] != '/') {
-       pattern = xmlStrdup((xmlChar *)"//");
-       pattern = xmlStrcat(pattern, key->match);
-    } else {
-       pattern = xmlStrdup(key->match);
+    /*
+     * Split the | and register it as as many keys
+     */
+    current = end = 0;
+    while (match[current] != 0) {
+       start = current;
+       while (IS_BLANK(match[current]))
+           current++;
+       end = current;
+       while ((match[end] != 0) && (match[end] != '|')) {
+           end++;
+       }
+       if (current == end) {
+           xsltPrintErrorContext(NULL, style, inst);
+           xsltGenericError(xsltGenericErrorContext,
+                            "key pattern is empty\n");
+           style->errors++;
+           goto error;
+       }
+       if (match[start] != '/') {
+           pattern = xmlStrcat(pattern, (xmlChar *)"//");
+           if (pattern == NULL) {
+               style->errors++;
+               goto error;
+           }
+       }
+       pattern = xmlStrncat(pattern, &match[start], end - start);
+       if (pattern == NULL) {
+           style->errors++;
+           goto error;
+       }
+
+       if (match[end] == '|') {
+           pattern = xmlStrcat(pattern, (xmlChar *)"|");
+           end++;
+       }
+       current = end;
     }
+#ifdef WITH_XSLT_DEBUG_KEYS
+    xsltGenericDebug(xsltGenericDebugContext,
+       "   resulting pattern %s\n", pattern);
+#endif
     key->comp = xmlXPathCompile(pattern);
     if (key->comp == NULL) {
        xsltPrintErrorContext(NULL, style, inst);
@@ -274,6 +311,7 @@ xsltAddKey(xsltStylesheetPtr style, const xmlChar *name,
     }
     key->next = style->keys;
     style->keys = key;
+error:
     if (pattern != NULL)
        xmlFree(pattern);
     return(0);
index ace0005..4ae7833 100644 (file)
@@ -1499,10 +1499,11 @@ xsltParseStylesheetKey(xsltStylesheetPtr style, xmlNodePtr key) {
     }
 
     /*
-     * register the key
+     * register the keys
      */
     xsltAddKey(style, name, nameURI, match, use, key);
 
+
 error:
     if (use != NULL)
        xmlFree(use);
index 9461edc..b164bf8 100644 (file)
@@ -70,6 +70,7 @@ EXTRA_DIST =  \
        bug-66.xml \
        bug-68.xml \
        bug-69.xml \
+       bug-70.xml \
        character.xml \
        array.xml \
        items.xml
diff --git a/tests/docs/bug-70.xml b/tests/docs/bug-70.xml
new file mode 100644 (file)
index 0000000..b5080dd
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<div id="div111" href="div111href">
+<div id="div222" href="div222href" >
+<obj id="obj333" href="obj333href" ></obj>
+</div>
+<link linkend="div111"/>
+<link linkend="div222"/>
+<link linkend="obj333"/>
+</div>
+
index 170a5a0..795e1cc 100644 (file)
@@ -73,6 +73,7 @@ EXTRA_DIST = \
     bug-66.out bug-66.xsl \
     bug-68.out bug-68.xsl \
     bug-69.out bug-69.xsl \
+    bug-70.out bug-70.xsl \
     character.out character.xsl \
     character2.out character2.xsl \
     itemschoose.out itemschoose.xsl \
diff --git a/tests/general/bug-70.out b/tests/general/bug-70.out
new file mode 100644 (file)
index 0000000..ff5a4de
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+
+
+
+
+div111 div111href
+div222 div222href
+obj333 obj333href
+
diff --git a/tests/general/bug-70.xsl b/tests/general/bug-70.xsl
new file mode 100644 (file)
index 0000000..13947f0
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version='1.0'?>
+<xsl:stylesheet
+   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
+
+<xsl:key name="mykey" match="div|obj" use="@id" />
+
+<xsl:template match="link">
+   <xsl:value-of select="@linkend"/>
+   <xsl:text> </xsl:text>
+   <xsl:value-of select="key('mykey', @linkend)/@href" />
+</xsl:template>
+
+</xsl:stylesheet>