Escaping double quotation marks in XSL

View: New views
13 Messages — Rating Filter:   Alert me  

Escaping double quotation marks in XSL

by caporale :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have XSL inside Javascript.  The XML looks like this: <image>imagename.jpg</image>.  Any time that tag's there, the output should be <img src=\"imagename.jpg\" / >.  This template works, but it doesn't escape the quotation marks:

<xsl:template match="image">
<img>
<xsl:attribute name="src">
<xsl:apply-templates />
</xsl:attribute>
</img>
</xsl:template>

What do I need to do to escape the quotation marks from that?  I've seen about 400 examples on-line--none of which actually do what I want.

Thanks,
Joseph Caporale


Re: Escaping double quotation marks in XSL

by Glen Mazza :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What do you mean by "escape"? Using a backslash as you have below, or
replacing it with the macro """?
I think you want the latter--and if you do that, it should work just as
nicely as the < and > are doing for you right now.

HTH,
Glen

caporale schrieb:

> I have XSL inside Javascript.  The XML looks like this:
> <image>imagename.jpg</image>.  Any time that tag's there, the output should
> be <img src=\"imagename.jpg\" / >.  This template works, but it
> doesn't escape the quotation marks:
>
> <xsl:template match="image">
> <img>
> <xsl:attribute name="src">
> <xsl:apply-templates />
> </xsl:attribute>
> </img>
> </xsl:template>
>
> What do I need to do to escape the quotation marks from that?  I've seen
> about 400 examples on-line--none of which actually do what I want.
>
> Thanks,
> Joseph Caporale
>
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


Re: Escaping double quotation marks in XSL

by caporale :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

<xsl:template match="image">
<img>
<xsl:attribute name="src">
<xsl:apply-templates />
</xsl:attribute>
</img>
</xsl:template>

This XSL gives me something like <img src="picture.jpg"/>, which doesn't work, because the quotation marks aren't escaped.  It's inside Javascript, so it has to be <img src=\"picture.jpg\"/>

Replacing it with """ doesn't work for some reason, and I can't put a backslash in that template without getting an error.


Glen Mazza wrote:
What do you mean by "escape"? Using a backslash as you have below, or
replacing it with the macro """?
I think you want the latter--and if you do that, it should work just as
nicely as the < and > are doing for you right now.

HTH,
Glen

caporale schrieb:
> I have XSL inside Javascript.  The XML looks like this:
> <image>imagename.jpg</image>.  Any time that tag's there, the output should
> be <img src=\"imagename.jpg\" / >.  This template works, but it
> doesn't escape the quotation marks:
>
> <xsl:template match="image">
> <img>
> <xsl:attribute name="src">
> <xsl:apply-templates />
> </xsl:attribute>
> </img>
> </xsl:template>
>
> What do I need to do to escape the quotation marks from that?  I've seen
> about 400 examples on-line--none of which actually do what I want.
>
> Thanks,
> Joseph Caporale
>
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org

Re: Escaping double quotation marks in XSL

by Brian Minchau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This isn't an "[Announce]".    OK, I got that off my chest.

Assuming that you are using Xalan to do your serialization, one of the
fundamental problems you are probably having is that Xalan has decided to
output all attribute values surrounded by double quotation marks, " ,  not
single quotation marks, ' .   That decision is pretty deep in the code.
This way when it encounters a double quote in the value of the attribute
value it will escape it as " (doing its best to retain the validity of
the output being generated.

Of course there are plenty of ways to generate invalid HTML or XML with any
XSLT processor and <![CDATA[ ...    ]]> comes to mind, where you can put
just about anything in there, including unpaired less than signs.

Assuming that you are only interested in stream serialization, have you
thought of doing the serialization yourself for particular elements?  For
example, something like this:

<!-- self serialization of image elements with tag for image done in CDATA,
but delegate attributes serialization -->
<xsl:template match="image">
 <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
</xsl:template>

<!-- handle src attribute of an image element -->
<xsl:template match="image/@src>
<![CDATA[  src=\"]>><value-of select="."><![CDATA[\"]>>
</xsl:template>

I think this needs polishing, but it has hope.

- Brian



                                                                           
             caporale                                                      
             <tranceradi@hotma                                            
             il.com>                                                    To
                                       general@...              
             10/23/2007 05:20                                           cc
             PM                                                            
                                                                   Subject
                                       [Announce] Escaping double          
             Please respond to         quotation marks in XSL              
             general@...                                            
                   e.org                                                  
                                                                           
                                                                           
                                                                           
                                                                           





I have XSL inside Javascript.  The XML looks like this:
<image>imagename.jpg</image>.  Any time that tag's there, the output should
be <img src=\"imagename.jpg\" / >.  This template works, but it
doesn't escape the quotation marks:

<xsl:template match="image">

<xsl:attribute name="src">
<xsl:apply-templates />
</xsl:attribute>
</img>
</xsl:template>

What do I need to do to escape the quotation marks from that?  I've seen
about 400 examples on-line--none of which actually do what I want.

Thanks,
Joseph Caporale


--
View this message in context:
http://www.nabble.com/Escaping-double-quotation-marks-in-XSL-tf4680435.html#a13373968

Sent from the Apache XML - General mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...




---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


Re: Escaping double quotation marks in XSL

by keshlam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Assuming that you are only interested in stream serialization, have you
> thought of doing the serialization yourself for particular elements?  For
> example, something like this:
>
> <!-- self serialization of image elements with tag for image done in
> CDATA,
but delegate attributes serialization -->
> <xsl:template match="image">
>  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
> </xsl:template>


I *REALLY* detest that idiom. I've seen many people hurt themselves by trying to hand-generate XML; I consider it a very bad practice. Unless you're forced to do it in order to work with a downstream tool which has not been implemented correctly, I would recommend finding another solution. Any other solution.


RE: Escaping double quotation marks in XSL

by Timothy Jones-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maybe you should explore something like this:
 
    <xsl:element name='image'>
        <xsl:for-each select='@*'>
            <xsl:attribute name='qname(.)'><xsl:value-of select='.' /></xsl:attribute>
        </xsl:for-each>
    </xsl:element>
 
This way, the transformer is at least aware of the tag you are trying to insert into the output.
 
 
 
tlj       


From: keshlam@... [mailto:keshlam@...]
Sent: Wednesday, October 24, 2007 11:58 AM
To: Brian Minchau
Cc: general@...; tranceradi@...; xalan-j-users@...
Subject: Re: [Announce] Escaping double quotation marks in XSL

> Assuming that you are only interested in stream serialization, have you
> thought of doing the serialization yourself for particular elements?  For
> example, something like this:
>
> <!-- self serialization of image elements with tag for image done in
> CDATA,
but delegate attributes serialization -->
> <xsl:template match="image">
>  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
> </xsl:template>


I *REALLY* detest that idiom. I've seen many people hurt themselves by trying to hand-generate XML; I consider it a very bad practice. Unless you're forced to do it in order to work with a downstream tool which has not been implemented correctly, I would recommend finding another solution. Any other solution.


Re: Escaping double quotation marks in XSL

by Brian Minchau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't like the idiom either. I agree that any other solution would be
better.  It made my stomach turn to suggest it.

Other suggestions for a solution?


- Brian



                                                                           
             keshlam@...                                            
             m                                                            
                                                                        To
             10/24/2007 11:58          Brian Minchau/Toronto/IBM@IBMCA    
             AM                                                         cc
                                       general@...,            
                                       tranceradi@...,            
             Please respond to         xalan-j-users@...        
             general@...                                     Subject
                   e.org               Re: [Announce] Escaping double      
                                       quotation marks in XSL              
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




> Assuming that you are only interested in stream serialization, have you
> thought of doing the serialization yourself for particular elements?  For
> example, something like this:
>
> <!-- self serialization of image elements with tag for image done in
> CDATA,
but delegate attributes serialization -->
> <xsl:template match="image">
>  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
> </xsl:template>

I *REALLY* detest that idiom. I've seen many people hurt themselves by
trying to hand-generate XML; I consider it a very bad practice. Unless
you're forced to do it in order to work with a downstream tool which has
not been implemented correctly, I would recommend finding another solution.
Any other solution.


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


RE: Escaping double quotation marks in XSL

by Brian Minchau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Timothy,
well, .... yes, .... but I think that trying to inject double quotes into
the attribute value (with modifications to you suggestion)  in this way
will only get them escaped as " which is not what the user wanted.

- Brian



                                                                           
             "Timothy Jones"                                              
             <Timothy.Jones@sy                                            
             niverse.com>                                               To
                                       <keshlam@...>, Brian        
             10/24/2007 12:14          Minchau/Toronto/IBM@IBMCA          
             PM                                                         cc
                                       <general@...>,          
                                       <tranceradi@...>,          
                                       <xalan-j-users@...>      
                                                                   Subject
                                       RE: [Announce] Escaping double      
                                       quotation marks in XSL              
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Maybe you should explore something like this:

    <xsl:element name='image'>
        <xsl:for-each select='@*'>
            <xsl:attribute name='qname(.)'><xsl:value-of select='.'
/></xsl:attribute>
        </xsl:for-each>
    </xsl:element>

This way, the transformer is at least aware of the tag you are trying to
insert into the output.



tlj

From: keshlam@... [mailto:keshlam@...]
Sent: Wednesday, October 24, 2007 11:58 AM
To: Brian Minchau
Cc: general@...; tranceradi@...;
xalan-j-users@...
Subject: Re: [Announce] Escaping double quotation marks in XSL



> Assuming that you are only interested in stream serialization, have you
> thought of doing the serialization yourself for particular elements?  For
> example, something like this:
>
> <!-- self serialization of image elements with tag for image done in
> CDATA,
but delegate attributes serialization -->
> <xsl:template match="image">
>  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
> </xsl:template>

I *REALLY* detest that idiom. I've seen many people hurt themselves by
trying to hand-generate XML; I consider it a very bad practice. Unless
you're forced to do it in order to work with a downstream tool which has
not been implemented correctly, I would recommend finding another solution.
Any other solution.




---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


Re: Escaping double quotation marks in XSL

by keshlam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How Hard Would It Be (how much performance would it cost us) if we put in a switch for whether the users wanted double-quoted attributes (requiring double-quotes within them be escaped) or single-quoted (requiring single-quotes within them be escaped)?

I know we don't want to try to switch on a case-by-case basis -- that would be a definite performance hit -- but this *might* be an affordable option and might help a few folks.

On the other hand, it might help too few to be justifiable. In which case the answer might be "It's open source; here's where to patch it to swap the two."

______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
-- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (http://www.ovff.org/pegasus/songs/threes-rev-11.html)


RE: Escaping double quotation marks in XSL

by Timothy Jones-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the detested example
    > <xsl:template match="image">
    >  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
    > </xsl:template>
I didn't see any attempt to inject double quotes (I'll even admit to not reading the subject line!).
 
But for inserting arbitrary characters (even metacharacters) without escaping, maybe the disable-output-escaping attribute of <xsl:value-of> and <xsl:text> would be useful.  In my application I have an XSL-generated HTML page with hyperlinks with GET parameters, and I use <xsl:value-of disable-output-escaping='yes'> to prevent the & characters in my GET URLs from being converted to &amp;.  Since = -> &quot; is a similar encoding to & -> &amp;, wouldn't this help?
 
I apologize if I am missing the point...
 
 
tlj
-----Original Message-----
From: Brian Minchau [
minchau@...]
Sent: Wednesday, October 24, 2007 12:31 PM
To: Timothy Jones
Cc: general@...; tranceradi@...; xalan-j-users@...
Subject: RE: [Announce] Escaping double quotation marks in XSL

Timothy,
well, .... yes, .... but I think that trying to inject double quotes into the attribute value (with modifications to you suggestion)  in this way will only get them escaped as &quot; which is not what the user wanted.

- Brian

                                                                          
                                                                          
                                                                          
                                                                          





RE: Escaping double quotation marks in XSL

by Brian Minchau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Timothy,
the original detested example was this:

<!-- self serialization of image elements with tag for image done in CDATA,
but delegate attributes serialization -->
<xsl:template match="image">
 <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
</xsl:template>

<!-- handle src attribute of an image element -->
<xsl:template match="image/@src>
<![CDATA[  src=\"]>><value-of select="."><![CDATA[\"]>>
</xsl:template>

Note in the template rule for the src attribute, the leading CDATA has
src=\"
and the losing CDATA has  \"
which is the injected and escaped (backslash before each one) double
quotes.

Joseph Caporale: are you still following this thread? You started it going.



caporale wrote:
> I have XSL inside Javascript.  The XML looks like this:
> <image>imagename.jpg</image>.  Any time that tag's there, the output
should

> be <img src=\"imagename.jpg\" / >.  This template works, but it
> doesn't escape the quotation marks:
>
> <xsl:template match="image">
> <img>
> <xsl:attribute name="src">
> <xsl:apply-templates />
> </xsl:attribute>
> </img>
> </xsl:template>
>
> What do I need to do to escape the quotation marks from that?  I've seen
> about 400 examples on-line--none of which actually do what I want.
>
> Thanks,
> Joseph Caporale


- Brian
- - - - - - - - - - - - - - - - - - - -
Brian Minchau, Ph.D.
XSLT Development, IBM Toronto
(780) 431-2633
e-mail:        minchau@...



                                                                           
             "Timothy Jones"                                              
             <Timothy.Jones@sy                                            
             niverse.com>                                               To
                                       Brian Minchau/Toronto/IBM@IBMCA    
             10/24/2007 12:52                                           cc
             PM                        <general@...>,          
                                       <tranceradi@...>,          
                                       <xalan-j-users@...>      
                                                                   Subject
                                       RE: [Announce] Escaping double      
                                       quotation marks in XSL              
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




In the detested example
    > <xsl:template match="image">
    >  <![CDATA[ <image]>><xsl:apply-templates select="@*"/> <![CDATA[ >]>>
    > </xsl:template>
I didn't see any attempt to inject double quotes (I'll even admit to not
reading the subject line!).

But for inserting arbitrary characters (even metacharacters) without
escaping, maybe the disable-output-escaping attribute of <xsl:value-of> and
<xsl:text> would be useful.  In my application I have an XSL-generated HTML
page with hyperlinks with GET parameters, and I use <xsl:value-of
disable-output-escaping='yes'> to prevent the & characters in my GET URLs
from being converted to &.  Since = -> " is a similar encoding to
& -> &, wouldn't this help?

I apologize if I am missing the point...


tlj
-----Original Message-----
From: Brian Minchau [mailto:minchau@...]
Sent: Wednesday, October 24, 2007 12:31 PM
To: Timothy Jones
Cc: general@...; tranceradi@...;
xalan-j-users@...
Subject: RE: [Announce] Escaping double quotation marks in XSL

Timothy,
well, .... yes, .... but I think that trying to inject double quotes into
the attribute value (with modifications to you suggestion)  in this way
will only get them escaped as " which is not what the user wanted.

- Brian











---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


Re: Escaping double quotation marks in XSL

by Brian Minchau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Joe K.,

I don't think that Joseph Caporale would be have his problem solved by the
choice of whether attribute values should be surrounded by double quotes,
or single quotes.

He wrote:
<<<
The XML looks like this:
<image>imagename.jpg</image>.  Any time that tag's there, the output should
be <img src=\"imagename.jpg\" / >.
>>>

He wants the " untouched, and he wants a \ infront of each ", and he
doesn't even want the <,> around the img tag.


- Brian



                                                                           
             keshlam@...                                            
             m                                                            
                                                                        To
             10/24/2007 12:40          Brian Minchau/Toronto/IBM@IBMCA    
             PM                                                         cc
                                       general@...,            
                                       tranceradi@...,            
             Please respond to         xalan-j-users@...        
             general@...                                     Subject
                   e.org               Re: [Announce] Escaping double      
                                       quotation marks in XSL              
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




How Hard Would It Be (how much performance would it cost us) if we put in a
switch for whether the users wanted double-quoted attributes (requiring
double-quotes within them be escaped) or single-quoted (requiring
single-quotes within them be escaped)?

I know we don't want to try to switch on a case-by-case basis -- that would
be a definite performance hit -- but this *might* be an affordable option
and might help a few folks.

On the other hand, it might help too few to be justifiable. In which case
the answer might be "It's open source; here's where to patch it to swap the
two."

______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
-- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (
http://www.ovff.org/pegasus/songs/threes-rev-11.html)


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@...
For additional commands, e-mail: general-help@...


Re: Escaping double quotation marks in XSL

by keshlam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> He wants the " untouched, and he wants a \ infront of each ", and he
> doesn't even want the <,> around the img tag.


So what he wants really is a block of text , not an element at all...

<xsl:template match="image">
<xsl:text>&amp;lt;img src=\"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>\" / &amp;gt;</xsl:text>
</xsl:template>

Right? Literal boilerplate surrounding the value extracted from the <image> element.

LightInTheBox - Buy quality products at wholesale price