You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Tomasz Nowak <tn...@netventure.pl> on 2003/05/19 17:00:20 UTC
ESQL query with ' and " special chars
DESCRIPTION OF THE SITATION:
============================
I have an article in xml format:
---XML----------------------------------------------------
<article id="123">
<title>Autobusem przez Pakistan</title>
<body>Someones something</body>
</article>
----------------------------------------------------------
which I want to insert into database with a help of Cocoon 2.0.4.
I've created an XSL stylesheet, which does an XSLTransformation
on that XML and transformes it into an XSP:
---XSL-----------------------------------------------------
[...]
<esql:query>
INSERT INTO articles (id, title, body)
VALUES (
<xsl:value-of select="@id"/>,
'<xsl:value-of select="title"/>',
'<xsl:apply-templates select="body"/>'
)
</esql:query>
[...]
<xsl:template match="body">
<body><xsl:apply-templates/></body>
</xsl:template>
[...]
----------------------------------------------------------
And I get from that XSLT an XSP file such as:
---XSP----------------------------------------------------
[...]
<esql:execute-query>
<esql:query>
INSERT
INTO articles (id, title, body)
VALUES (
123,
'This is a title',
'<body>Someones something</body>'
)
</esql:query>
</esql:execute-query>
[...]
----------------------------------------------------------
After invoking that XSP I see correctly inserted into
database news record:
id | title | body
-------------------------------------------------------
123 | This is a title | <body>Someones something</body>
A PROBLEM
=========
Untill now I have no problem with that.
But *IF* there are two of five XML special chars
in source XML: " and ' - that esql query
doesn't work!
---bad-XML-v1----------------------------------------------
<article id="123">
<title>Autobusem przez Pakistan</title>
<body>Someone's "something"</body>
</article>
----------------------------------------------------------
or even
---bad-XML-v2----------------------------------------------
<article id="123">
<title>Autobusem przez Pakistan</title>
<body>Someone's "something"</body>
</article>
----------------------------------------------------------
I get XSP:
---XSP----------------------------------------------------
[...]
<esql:execute-query>
<esql:query>
INSERT
INTO articles (id, title, body)
VALUES (
123,
'This is a title',
'<body>Someone's "something"</body>'
)
</esql:query>
</esql:execute-query>
[...]
----------------------------------------------------------
Which doesn't go throught database correcty because of a ' sign.
What I have already tried:
1. Putting other 3 special chars (<, > and &) into that XML
causes _no problems_ because they are correctly escaped.
2. I've also tried to hard-code Javascript-style escapes:
'<body>Someone\'s \"something\"</body>'
- that also doesn't help.
I would appreciate any help on this problem.
The issue is urnet for me.
Thanks in advance.
--
Tomasz Nowak
---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org
Re: ESQL query with ' and " special chars
Posted by Christian Haul <ha...@informatik.tu-darmstadt.de>.
Tomasz Nowak wrote:
> DESCRIPTION OF THE SITATION:
> ============================
>
> I have an article in xml format:
>
> ---XML----------------------------------------------------
> <article id="123">
> <title>Autobusem przez Pakistan</title>
> <body>Someones something</body>
> </article>
>
> ----------------------------------------------------------
>
> which I want to insert into database with a help of Cocoon 2.0.4.
> I've created an XSL stylesheet, which does an XSLTransformation
> on that XML and transformes it into an XSP:
<snip/>
> A PROBLEM
> =========
>
> Untill now I have no problem with that.
> But *IF* there are two of five XML special chars
> in source XML: " and ' - that esql query
> doesn't work!
>
> ---bad-XML-v1----------------------------------------------
> <article id="123">
> <title>Autobusem przez Pakistan</title>
> <body>Someone's "something"</body>
> </article>
Put the xml into a java var and use <esql:param/> (esql:parameter?)
to pass it as parameter of a prepared statement. The driver would cater
for the escaping then. Constructing a valid java string might become a
new problem, though. You might want to look into the util logicsheet.
Chris.
--
C h r i s t i a n H a u l
haul@informatik.tu-darmstadt.de
fingerprint: 99B0 1D9D 7919 644A 4837 7D73 FEF9 6856 335A 9E08
---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org
Re: ESQL query with ' and " special chars
Posted by Oliver Burnett-Hall <ol...@blueyonder.co.uk>.
On Mon, 2003-05-19 at 16:00, Tomasz Nowak wrote:
<snip>
> I get XSP:
>
> ---XSP----------------------------------------------------
> [...]
> <esql:execute-query>
> <esql:query>
> INSERT
> INTO articles (id, title, body)
> VALUES (
> 123,
> 'This is a title',
> '<body>Someone's "something"</body>'
> )
> </esql:query>
> </esql:execute-query>
> [...]
> ----------------------------------------------------------
>
> Which doesn't go throught database correcty because of a ' sign.
There's probably an easy and elegant solution to this, but I don't know
it. I can give you a brute force instead.
If your <body> element just contains text, the following templates will
transform it and escape any apostrophes present, replacing them with
' character entities:
<xsl:template match="body">
<xsl:text><body></xsl:text>
<xsl:call-template name="escape-apos">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text></body></xsl:text>
</xsl:template>
<xsl:template name="escape-apos">
<xsl:param name="text"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:choose>
<xsl:when test="contains($text, $apos)">
<xsl:value-of select="substring-before(
$text, $apos)"/>
<xsl:text disable-output-escaping="yes">&apos;</xsl:text>
<xsl:call-template name="escape-apos">
<xsl:with-param name="text" select="substring-after(
$text, $apos)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Double quotes shouldn't be a problem, so you can just ignore them.
HTH,
- olly
---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org