You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Miguel Carvalho <mi...@mail.telepac.pt> on 2003/08/12 13:24:37 UTC

problem accessing attribute in xsl with cocoon

HI, i'm having a little trouble in putting a transformer in xsl to work with
cocoon.
The thing is, after i apply a java trasformer made by me i get the following
result in the "view source" of IE

<!-- XML FRAGMENT -->

<document>

<contents>

<article layouttype="2" xpos="5" ypos="7" border="dotted">

<title>Titulo com rating 2</title>

<text>Texto</text>

</article>

<article layouttype="3" xpos="5" ypos="7" border="dotted">

<title>Titulo do artigo com rating igual a 1</title>

<text>texto do artigo com rating igual a 1</text>

<image>img1.jpg</image>

</article>

<article layouttype="4" xpos="5" ypos="7" border="dotted">

<title>Titulo do artigo com rating igual a 2</title>

<text>texto do artigo com rating igual a 2</text>

<image>img1.jpg</image>

</article>

<article layouttype="2" xpos="5" ypos="7" border="dotted">

<title>Titulo do artigo com rating igual a 2</title>

<text>texto do artigo com rating igual a 2</text>

<image>img1.jpg</image>

</article>

</contents>

</document>


and this is good.

After this i want to apply another generator in xsl,


<!-- XLS STYLESHEET daeTransformer2html.xsl -->

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output type="xml"/>

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="contents">
        <HTML>
            <HEAD>
            </HEAD>
            <BODY>
                <xsl:apply-templates select="article[@layouttype='2']"/>
            </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="article">
        article
        <xsl:value-of select="title"/>
        <xsl:value-of select="text"/>

        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="image"/>
            </xsl:attribute>
        </img>

    </xsl:template>

</xsl:stylesheet>

The sitemap defined is as follows

<!-- SITEMAP -->

<map:match pattern="daestart">
        <map:generate type="serverpages" src="daexsps/start.xsp"/>
        <map:transform src="daestyles/removens.xsl"/>
        <map:transform type="dae"/>
        <map:transform src="daestyles/daeTransformer2html.xsl"/>
        <map:serialize type="html"/>
    </map:match>

and the answer sent to the browser is

<!-- RESPONSE SENT TO BROWSER -->
        <HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</HEAD>
<BODY></BODY>
</HTML>

wich is not right.
I tried to copy the first XML Fragment and put it in a file with the name
daexmltest.xml and apply the XSL referenced earlier (pipeline bellow)

<!-- PIPELINE -->
<map:match pattern="daetest">
        <map:generate src="daexmltest.xml"/>
         <map:transform src="daestyles/daeTransformer2html.xsl"/>
        <map:serialize type="html"/>
    </map:match>

and the response to the browser was

    <HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</HEAD>
<BODY>
        article
        Titulo com rating 2Texto<img src="">
        article
        Titulo do artigo com rating igual a 2texto do artigo com rating
igual a 2<img src="img1.jpg"></BODY>
</HTML>

and this is rigth, because it accesses the attribute @layouttype and returns
only the <article> tags that has @layouttype=2


If you have any ideas , of how to handle this ...

Thanks in advance
Miguel Carvalho



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: cocoon sax events and transformer doubts

Posted by Bruno Dumon <br...@outerthought.org>.
On Tue, 2003-08-12 at 17:04, Bruno Dumon wrote:
> On Tue, 2003-08-12 at 17:02, Miguel Carvalho wrote:
> <snip/>
> >                 newAttr.addAttribute("", localName, "layouttype", "", "2");
> >                 newAttr.addAttribute("", localName, "xpos", "", "5");
> >                 newAttr.addAttribute("", localName, "ypos", "", "7");
> >                 newAttr.addAttribute("", localName, "border", "", "dotted");
> 
> This is likely the problem: the localName should also be the attribute
> name, so in fact you need to supply the same string twice:
> 
> newAttr.addAttribute("", "layouttype", "layouttype", "", "2");

oops, looked over another problem: the 4th argument should be "CDATA":

newAttr.addAttribute("", "layouttype", "layouttype", "CDATA", "2");

-- 
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org                          bruno@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: cocoon sax events and transformer doubts

Posted by Miguel Carvalho <mi...@mail.telepac.pt>.
Thanks... Right on the spot... :)


Miguel Carvalho

-----Original Message-----
From: Bruno Dumon [mailto:bruno@outerthought.org]
Sent: terça-feira, 12 de Agosto de 2003 16:04
To: users@cocoon.apache.org
Subject: Re: cocoon sax events and transformer doubts


On Tue, 2003-08-12 at 17:02, Miguel Carvalho wrote:
<snip/>
>                 newAttr.addAttribute("", localName, "layouttype", "",
"2");
>                 newAttr.addAttribute("", localName, "xpos", "", "5");
>                 newAttr.addAttribute("", localName, "ypos", "", "7");
>                 newAttr.addAttribute("", localName, "border", "",
"dotted");

This is likely the problem: the localName should also be the attribute
name, so in fact you need to supply the same string twice:

newAttr.addAttribute("", "layouttype", "layouttype", "", "2");

In this case the two arguments are the same because you're using an
empty namespace. Otherwise, the second argument would contain the
namespace prefix.

--
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org                          bruno@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: cocoon sax events and transformer doubts

Posted by Bruno Dumon <br...@outerthought.org>.
On Tue, 2003-08-12 at 17:02, Miguel Carvalho wrote:
<snip/>
>                 newAttr.addAttribute("", localName, "layouttype", "", "2");
>                 newAttr.addAttribute("", localName, "xpos", "", "5");
>                 newAttr.addAttribute("", localName, "ypos", "", "7");
>                 newAttr.addAttribute("", localName, "border", "", "dotted");

This is likely the problem: the localName should also be the attribute
name, so in fact you need to supply the same string twice:

newAttr.addAttribute("", "layouttype", "layouttype", "", "2");

In this case the two arguments are the same because you're using an
empty namespace. Otherwise, the second argument would contain the
namespace prefix.

-- 
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org                          bruno@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


cocoon sax events and transformer doubts

Posted by Miguel Carvalho <mi...@mail.telepac.pt>.
Hi, i'm having some doubts about the way the sax events are created in a
transformer in java...

I'm asking this question because i wrote a transformer in java, but i want
to apply another transformer in xsl after that and i can't access the
attributes in the nodes...

this is the code of the transformer i wrote..

package pt.laseeb.dae.transformer;

/*
 * File: daeTransformer.java
 *
 * Project: Design Automatico e Evolutivo
 *
 * Authors: Paulo Sérgio Silva Paixao        - pssp@mega.ist.utl.pt
 *          Miguel Angelo Rodrigues Carvalho - marc@mega.ist.utl.pt
 *
 * Date: 4/Ago/2003
 *
 * Trabalho de Fim de Curso - Design Automático e Evolutivo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

import org.apache.cocoon.transformation.AbstractTransformer;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.ProcessingException;
import org.apache.avalon.framework.parameters.Parameters;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import java.util.Map;
import java.io.IOException;

public class daeTransformer extends AbstractTransformer
{
    public static final String DOCUMENT_ELEMENT = "document";
    public static final String CONTENT_ELEMENT = "contents";
    public static final String ARTICLE_ELEMENT = "article";
    public static final String ARTICLE_TITLE_ELEMENT = "title";
    public static final String ARTICLE_TEXT_ELEMENT = "text";
    public static final String ARTICLE_IMAGE_ELEMENT = "image";

    protected static final int MODE_NONE = 0;
    protected static final int MODE_ARTICLE = 1;
    protected static final int MODE_TITLE = 2;
    protected static final int MODE_TEXT = 3;
    protected static final int MODE_IMAGE = 4;

    protected int mode;
    protected StringBuffer articleText;
    protected StringBuffer articleTitle;
    protected StringBuffer articleImage;

    protected Attributes attr;


    public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters par)
            throws ProcessingException, SAXException, IOException
    {
        this.mode = MODE_NONE;
        this.articleImage = new StringBuffer();
        this.articleTitle = new StringBuffer();
        this.articleText = new StringBuffer();
    }


    public void startElement(String namespaceURI, String localName, String
qName,
            Attributes attributes) throws SAXException
    {
            AttributesImpl newAttr = new AttributesImpl();

            if (localName.equals(DOCUMENT_ELEMENT) == true)
            {
                super.startElement("", localName , qName, newAttr);
            }
            else if (localName.equals(CONTENT_ELEMENT) == true)

            {
                super.startElement("", localName , qName, newAttr);

            }
            else if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                newAttr.addAttribute("", localName, "layouttype", "", "2");
                newAttr.addAttribute("", localName, "xpos", "", "5");
                newAttr.addAttribute("", localName, "ypos", "", "7");
                newAttr.addAttribute("", localName, "border", "", "dotted");

                super.startElement("", localName, qName, newAttr);

            }
            else if (localName.equals(ARTICLE_TITLE_ELEMENT) == true)
            {
                this.mode = MODE_TITLE;

                super.startElement("", localName, qName, newAttr);
            }
            else if (localName.equals(ARTICLE_TEXT_ELEMENT) == true)
            {
                this.mode = MODE_TEXT;
                super.startElement("", localName, qName, attributes);
            }
            else if (localName.equals(ARTICLE_IMAGE_ELEMENT) == true)
            {
                this.mode = MODE_IMAGE;
                super.startElement("", localName, qName, attributes);
            }
            else
            {
                throw new SAXException("Unknown start element " +
localName);
            }
    }


    public void characters(char[] buffer, int start, int length)
            throws SAXException
    {
        switch (this.mode)
        {
            case MODE_NONE : super.characters(buffer, start, length);
            break;
            case MODE_TITLE : this.articleTitle.append(buffer, start,
length);
            break;
            case MODE_TEXT : this.articleText.append(buffer, start, length);
            break;
            case MODE_IMAGE : this.articleImage.append(buffer, start,
length);
            break;
        }
    }

    public void endElement(String namespaceURI, String localName, String
qName)
            throws SAXException {


        if (localName.equals(DOCUMENT_ELEMENT) == true)
            {
                super.endElement("", localName , qName);
            }
            else if (localName.equals(CONTENT_ELEMENT) == true)
            {
                super.endElement("", localName, qName);
            }
            else if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                super.endElement("", localName, qName);

            } else if (localName.equals(ARTICLE_TITLE_ELEMENT) == true)
            {
    // mailto received
                super.characters(this.articleTitle.toString().toCharArray(),
0, this.articleTitle.length());
                this.articleTitle = null;
                this.articleTitle = new StringBuffer();
                super.endElement("", localName, qName);
                this.mode = MODE_NONE;

            } else if (localName.equals(ARTICLE_TEXT_ELEMENT) == true)
            {
                super.characters(this.articleText.toString().toCharArray(),
0, this.articleText.length());
                this.articleText = null;
                this.articleText = new StringBuffer();
                super.endElement("", localName, qName);
                this.mode = MODE_NONE;

            } else if (localName.equals(ARTICLE_IMAGE_ELEMENT) == true)
            {
                super.characters(this.articleImage.toString().toCharArray(),
0, this.articleImage.length());
                this.articleImage = null;
                this.articleImage = new StringBuffer();
                super.endElement("", localName, qName);
                this.mode = MODE_NONE;

            } else
            {
                throw new SAXException("Unknown end element " + localName);
            }
    }

}


and the result of this transformer is something like ...


<document>

<contents>

<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo com rating 2</title>
<text>Texto</text>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 1</title>
<text>texto do artigo com rating igual a 1</text>
<image>img1.jpg</image>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 2</title>
<text>texto do artigo com rating igual a 2</text>
<image>img1.jpg</image>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 2</title>
<text>texto do artigo com rating igual a 2</text>
<image>img1.jpg</image>
</article>

</contents>

</document>


and after that i would like to apply a xsl stylesheet that can access some
of the properties available in the nodes, this is the code of the xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output type="html"/>

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

    <xsl:template match="contents">
        contents
        <HTML>
            <HEAD>
            </HEAD>
            <BODY>
                <xsl:apply-templates select="article[@layouttype='2']"/>
            </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="border">
        border
    </xsl:template>

    <xsl:template match="article">
        article
        <xsl:value-of select="title"/>
        <xsl:value-of select="text"/>

        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="image"/>
            </xsl:attribute>
        </img>

    </xsl:template>

</xsl:stylesheet>

but i can't access the attributes of the nodes...


If anyone has some ideas of why this might be hapenning it would be a very
good help..


thanks in advance
Miguel Carvalho



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org