You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Robin Green <gr...@hotmail.com> on 2000/08/16 13:58:21 UTC

[PATCH] and fix Re: Trouble with namespace

Alexander Krasilnikov <we...@neonweb.ru> wrote:
>       I have strange trouble with namespace's. Small example:

I had a similar problem. The undocumented workaround is to replace

<em:page xmlns:em="http://west/">

with

<em:page xsp:em="http://west/">

The xsp: namespace prefix acts as a surrogate xmlns: and is read in and 
corrected by the XSP processor. The need for this is due to a frankly 
patronising flaw in the XSLT specification, where they decided that 
stylesheet authors should not be able to access xmlns attributes - I don't 
know why - presumably because there was "too much potential for 
confusion/abuse". Sigh.

Since the XSP processor uses a logicsheet (xsp-java.xsl) to do most of the 
xsp transformations, it is hampered by this limitation. However, I have a 
(not-very-much-tested) patch here which should allow you to use xmlns: in 
content elements (i.e. anything except the xsp:page element, which is 
reserved for logicsheet namespaces), in the standard way, and the 
substitition would be performed automatically.

This patch is primarily for the attention of the cocoon committers (if there 
are any not on vacation! :), so you can ignore it and just use the 
workaround if you wish.


--- 
/dannii/usr/local/cocoon/src/org/apache/cocoon/processor/xsp/language/java/XSPJavaPreprocessor.java 
   Mon Jun 19 11:51:40 2000
+++ 
/dannii/home/users/greenrd/xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/XSPJavaPreprocessor.java 
      Wed Aug 16 11:56:14 2000
@@ -60,7 +60,8 @@

/**
  * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
- * @version $Revision: 1.5 $ $Date: 2000/03/22 17:44:53 $
+ * @author <a href="mailto:greenrd@hotmail.com">Robin Green</a>
+ * @version $Revision: 1.6 $ $Date: 2000/08/16 11:53:53 $
  */
public class XSPJavaPreprocessor implements XSPPreprocessor {
   protected static XSPJavaProcessor javaProcessor = new XSPJavaProcessor();
@@ -112,8 +113,34 @@
         parent.replaceChild(textElement, node);

         break;
+
+      case Node.ATTRIBUTE_NODE:
+        // Hack to be able to copy over xmlns declaractions
+        // which cannot be matched by any XSLT pattern
+        Attr attr = (Attr) node;
+        String attName = attr.getName ();
+        if (attName.startsWith ("xmlns:")) {
+          // Replace with xsp:... which will be picked up
+          // by the xsp-java.xsl logicsheet
+          Element ownerE = attr.getOwnerElement ();
+          ownerE.setAttribute
+            ("xsp:" + attName.substring (6), attr.getValue ());
+          ownerE.removeAttribute (attName);
+        }
+        break;
+
       case Node.ELEMENT_NODE:
         ((Element) node).normalize();
+
+        if (!node.getNodeName ().equals ("xsp:page")) {
+          NamedNodeMap map = node.getAttributes ();
+          int attCount = map.getLength ();
+          for (int j = 0; j < attCount; j++) {
+            Node n = map.item(j);
+            if (n != null) this.process(n);
+          }
+        }                                                               +
         // Fall through
       default:
         NodeList childList = node.getChildNodes();



--
Robin Green
i-tao Ltd.
4 Skyline Village
Limeharbour
London E14 9TS
United Kingdom
Phone +44 20 7537 2233  Fax +44 70 8081 5118
http://www.i-tao.com


________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


Re: [PATCH] and fix Re: Trouble with namespace

Posted by Ricardo Rocha <ri...@apache.org>.
Robin Green wrote:
> 
> Alexander Krasilnikov <we...@neonweb.ru> wrote:
> >       I have strange trouble with namespace's. Small example:
> 
> I had a similar problem. The undocumented workaround is to replace
> 
> <em:page xmlns:em="http://west/">
> 
> with
> 
> <em:page xsp:em="http://west/">
> 
> The xsp: namespace prefix acts as a surrogate xmlns: and is read in and
> corrected by the XSP processor. The need for this is due to a frankly
> patronising flaw in the XSLT specification, where they decided that
> stylesheet authors should not be able to access xmlns attributes - I don't
> know why - presumably because there was "too much potential for
> confusion/abuse". Sigh.
> 
> Since the XSP processor uses a logicsheet (xsp-java.xsl) to do most of the
> xsp transformations, it is hampered by this limitation. However, I have a
> (not-very-much-tested) patch here which should allow you to use xmlns: in
> content elements (i.e. anything except the xsp:page element, which is
> reserved for logicsheet namespaces), in the standard way, and the
> substitition would be performed automatically.

Yes, as usual, Robin is absolutely right.

I may have found a solution for this. In the last-step XSP
logicsheet we can rewrite the generic element-matching
template to read:

  <xsl:template match="*">
    xspParentNode = xspCurrentNode;
    xspNodeStack.push(xspParentNode);
    xspCurrentNode =
      document.createElement("<xsl:value-of select="name(.)"/>");
    xspParentNode.appendChild(xspCurrentNode);

    <!-- Begin namespace workaround -->
    <xsl:for-each select="namespace::*">
      ((Element) xspCurrentNode).setAttribute(
        "xmlns:<xsl:value-of select="local-name(.)"/>",
        "<xsl:value-of select="."/>"
      );
    </xsl:for-each>
    <!-- End namespace workaround -->

    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>

    ((Element) xspCurrentNode).normalize();
    xspCurrentNode = (Node) xspNodeStack.pop();
  </xsl:template>

This is more general than the current hack: it'll preserve namespace
declarations in every element, not just the root.

I've used a similar approach in XSP for Cocoon2 and it preserves
namespaces correctly.

Re: [PATCH] and fix Re: Trouble with namespace

Posted by Ricardo Rocha <ri...@apache.org>.
Namespaces are now preserved without requiring
re-declaration with "xsp:". Check out the latest
fix from the CVS.

Alexander Krasilnikov wrote:
> 
> Hello Robin,
> 
> Wednesday, August 16, 2000, 5:58:21 PM, you wrote:
> 
> RG> Alexander Krasilnikov <we...@neonweb.ru> wrote:
> >>       I have strange trouble with namespace's. Small example:
> 
> RG> I had a similar problem. The undocumented workaround is to replace
> RG> <em:page xmlns:em="http://west/">
> RG> with
> RG> <em:page xsp:em="http://west/">
> 
>        Thank you for your help kindly. It works, but not in all cases and i
>        can't understand what rule is used. I'll try further.
>        Any way can i hope this bug will be eliminated in next version
>        of Cocoon(Xalan or Xerces)?
> 
> --
> Best regards,
>  Alexander                            mailto:west@neonweb.ru
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: cocoon-users-help@xml.apache.org

Re: [PATCH] and fix Re: Trouble with namespace

Posted by Alexander Krasilnikov <we...@neonweb.ru>.
Hello Robin,

Wednesday, August 16, 2000, 5:58:21 PM, you wrote:

RG> Alexander Krasilnikov <we...@neonweb.ru> wrote:
>>       I have strange trouble with namespace's. Small example:

RG> I had a similar problem. The undocumented workaround is to replace
RG> <em:page xmlns:em="http://west/">
RG> with
RG> <em:page xsp:em="http://west/">

       Thank you for your help kindly. It works, but not in all cases and i
       can't understand what rule is used. I'll try further.
       Any way can i hope this bug will be eliminated in next version
       of Cocoon(Xalan or Xerces)?

-- 
Best regards,
 Alexander                            mailto:west@neonweb.ru