You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Aaron VonderHaar <av...@umich.edu> on 2000/10/14 19:14:11 UTC
Re: XSP is encoding an XML string generated by Java code
The problem is that Strings are inserted into Document via a call to
org.w3c.dom.Document.createTextNode(String), which converts <>&" to
entities (< and so forth). The only way I know to get around this
is to parse in the string and appendChild it. Try this:
----- in page-xsp.xsl change String foofunc() -------------
private synchronized String foofunc(Document d, Node n) {
String retval = <![CDATA["<p><b>String</b> to be parsed</p>"]]>;
try {
n.appendChild(
d.importNode(
xspParser.parse(
new InputSource(
new StringReader(retval)
)).getDocumentElement(), true)
);
}
catch (org.xml.sax.SAXException e) { return e.getMessage(); }
catch (java.io.IOException e) { return e.getMessage(); }
return "";
}
----- in page-xsp.xsl change call to foofunc() -------------
<xsp:expr>foofunc(document, xspCurrentNode)</xsp:expr>
document and xspCurrentNode are defined by xsp to allow access to the
document structure (what we needed here).
NOTE: String retval must be well-formed xml -- that is, all tags must
close, and it must contain A SINGLE ROOT NODE with no text before or
after. ie retval="an <i>good</i> parser" will throw SAXException.
(However, this exception can be caught (as above) and the rest of the
document will recover though foofunc fails.)
When I worked all this out, I was trying to insert xml from an SQL db,
so I changed "new StringReader(value)" to
new StringReader("<X>"+value+"</X>")
and then had <X/> merely apply-templates in a subsequent xsl
transform. This way, the db entires didn't need root nodes.
Aaron VonderHaar
avonderh@umich.edu
>
> Given the following .xml and .xsp files [respectively]:
>
> ----- page.xml --------------------------------------------
>
> <?xml version="1.0"?>
> <?cocoon-process type="xslt"?>
> <?xml-stylesheet href="page-xsp.xsl" type="text/xsl"?>
> <page> <foobartag/> </page>
>
> ----- page-xsp.xsl ----------------------------------------
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xsp="http://www.apache.org/1999/XSP/Core">
>
> <xsl:template match="page">
> <xsl:processing-instruction
> name="cocoon-process">type="xsp"</xsl:processing-instruction>
>
> <xsp:page language="java"
> xmlns:xsp="http://www.apache.org/1999/XSP/Core">
> <xsp:logic>
> private synchronized String foofunc() {
> String retval =
> <![CDATA["<foobar><name>Asmodius</name><age>99yo</age><gender>F</gender></foobar>"]]>;
> return retval;
> }
> </xsp:logic>
>
> <xsl:copy>
> <xsl:apply-templates/>
> </xsl:copy>
>
> </xsp:page>
>
> </xsl:template>
>
> <xsl:template match="foobartag">
> <xsp:expr>foofunc()</xsp:expr>
> </xsl:template>
>
> </xsl:stylesheet>
>
> ---------------------------------------------------------
>
> what we get is:
> <foobar><name>Asmodius</name><age>
> 99yo</age><gender>F</gender></foobar>
>
> what we would like:
> <foobar><name>Asmodius</name><age>99yo</age><gender>F</gender>
> </foobar>
>
> The "foofunc()" is a mockup function that, in real life, will
> return data from some external datasource [such as MySQL].
> Prior to being returned from foofunc(), the data will be
> wrapped in XML tags. We would like to have the data remain
> "as is" for processing by a secondary XSL transformation,
> yielding the HTML product. But, as you can see, the
> beginning and ending brackets ("<", ">") are being converted
> to < and >.
>
> -Bob
> bob@mmrd.com <ma...@mmrd.com>
>