Re: [xslt] xsl:choose With Preserved Space



After a second tought and more careful investigation on what other 
processors do, I found the following.

Within a xsl:choose, only the children of xsl:when and the children of 
xsl:otherwise belong to the output document, be they whitespace or whatever.

The whitespace which is a sibling of xsl:when does not belong to the 
ouput document and should therefore not be copied to the output 
document. But, this whitespace should not be treated as an error, it 
should be ignored instead.

When xml:space isn't set to preserve, this whitespace will be stripped 
anyway. Otherwise, it will be there and the stylesheet will ignore it. 
This seems okay with the spec and matches the behaviour of other processors.

Here is a patch to transform.c which brings this in. If I am breaking 
something with this, scream, otherwise I'll commit.

Ciao,
Igor
RCS file: /cvs/gnome/libxslt/libxslt/transform.c,v
retrieving revision 1.216
diff -c -r1.216 transform.c
*** transform.c 10 May 2003 18:21:45 -0000      1.216
--- transform.c 7 Jun 2003 15:29:48 -0000
***************
*** 3208,3222 ****
             "xsl:choose: empty content not allowed\n");
        goto error;
      }
!     if ((!IS_XSLT_ELEM(replacement)) ||
!       (!IS_XSLT_NAME(replacement, "when"))) {
        xsltTransformError(ctxt, NULL, inst,
             "xsl:choose: xsl:when expected first\n");
        goto error;
      }
!     while (IS_XSLT_ELEM(replacement) && (IS_XSLT_NAME(replacement, "when"))) {
        xsltStylePreCompPtr wcomp = replacement->_private;

        if ((wcomp == NULL) || (wcomp->test == NULL) || (wcomp->comp == NULL)) {
            xsltTransformError(ctxt, NULL, inst,
                 "xsl:choose: compilation failed !\n");
--- 3208,3228 ----
             "xsl:choose: empty content not allowed\n");
        goto error;
      }
!     if (((!IS_XSLT_ELEM(replacement)) || (!IS_XSLT_NAME(replacement, "when")))
!           && (!xmlIsBlankNode(replacement))) {
        xsltTransformError(ctxt, NULL, inst,
             "xsl:choose: xsl:when expected first\n");
        goto error;
      }
!     while ((IS_XSLT_ELEM(replacement) && (IS_XSLT_NAME(replacement, "when")))
!           || xmlIsBlankNode(replacement)) {
        xsltStylePreCompPtr wcomp = replacement->_private;

+       if (xmlIsBlankNode(replacement)) {
+           replacement = replacement->next;
+           continue;
+       }
+
        if ((wcomp == NULL) || (wcomp->test == NULL) || (wcomp->comp == NULL)) {
            xsltTransformError(ctxt, NULL, inst,
                 "xsl:choose: compilation failed !\n");
***************
*** 3292,3297 ****
--- 3298,3306 ----
  #endif
        xsltApplyOneTemplate(ctxt, ctxt->node, replacement->children,
                             NULL, NULL);
+       replacement = replacement->next;
+     }
+     while (xmlIsBlankNode(replacement)) {
        replacement = replacement->next;
      }
      if (replacement != NULL) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]