This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes convert & to & Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "convert & to & " Watch "convert & to & " New topic
Author

convert & to &

Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
This could be one of the most common questions you can think of when dealing with xsl, but still I am unable to get past. Here's the scenario...

I have an xml attribute caption with value attribute that will be used as a value of "value" attribute of the JSF's command button. So, this caption element may contain something like "&" for &.
Ideally, on my JSP the button should be displayed as "Save & Exit", but it doesn't, rather it is displayed as "Save & Exit"

So the caption attribute's value in the xml is "Save & Exit".

Now, what I want is while xsl transformation is going on, this attribute value be changed to "Save & Exit" and corresponding JSF command button be generated as...



But, somehow, it still generates



I tried to use this template-



And the above template gets called from here




Any idea what's wrong here ? I am using XSL version 1.0, BTW. I can't also use escape attribute of JSF command button since the version we are working on doesn't support escape attribute.

Thanks.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Girish Vasmatkar wrote:I have an xml attribute caption with value attribute that will be used as a value of "value" attribute of the JSF's command button. So, this caption element may contain something like "&" for &.
Ideally, on my JSP the button should be displayed as "Save & Exit", but it doesn't, rather it is displayed as "Save & Exit"

So the caption attribute's value in the xml is "Save & Exit".


If you look at the title of your post, and read what you posted, it appears to be nonsense. (Read it yourself and see if you agree.) But it's really because you don't understand the escaping of ampersands in HTML and XML. And that escaping is at the root of your problem. So first you should learn how it actually works. If you want an ampersand to appear then you must use "&", which is the escaped form. And if you want "&" to appear in an escaped form -- as I did when I typed the first part of this sentence -- then you must use "&". Try that out for yourself by posting a response which appears the way you want it to appear.

And therefore your problem is unlikely to be solved by doing that replacement which you are trying to do. It's more likely to be solved by escaping things properly in the right place.

Unfortunately I don't have any suggestions because I can't tell what you are trying to do. (That's because you asked for help on your failed non-solution rather than on the original problem.) It looks to me like you're trying to generate JSF code via XSLT, but surely that can't be the case?
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
If you look at the title of your post, and read what you posted, it appears to be nonsense. (Read it yourself and see if you agree.) But it's really because you don't understand the escaping of ampersands in HTML and XML. And that escaping is at the root of your problem. So first you should learn how it actually works. If you want an ampersand to appear then you must use "&", which is the escaped form.


Yeah, I did that for most of the part of post (&) and I did for title too, but you know, now my thread's title appears wrong now on the list of the threads. Anyway, I am trying to generate the JSF page using XSLT.


And therefore your problem is unlikely to be solved by doing that replacement which you are trying to do. It's more likely to be solved by escaping things properly in the right place.


I don't think so, mere escaping can't be the solution here. I think the only way I can get the button to show "Save & Exit" on itself is to have this code in place-



which is only possible if the XSL code itself replaces the attribute value. Are you talking about

?

Which I don't think is the right choice here.

Thank you for the reply.
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
I may be wrong, but may be clarifying things will help-

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're really trying to generate JSF from XSLT? Wow.

But anyway the example you displayed:

isn't valid XML because the ampersand isn't escaped. It should look like this:


Which means that the disable-output-escaping attribute isn't what you want.
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Sadly enough, this is what our current implementation does and we've got to stick with it.

By the way, the



Would be the code in the JSP and right now the JSP that's generated looks something like this-



If this were the case with <hutputText/>, I'd have simply used escape="false", but commandButton doesn't have such attribute.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I see. So in effect the only HTML which works for you is invalid HTML (because the ampersand isn't escaped correctly)? And the invalid HTML actually results in different output than the valid HTML?
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Well, when this JSF code gets converted in the HTML (view source), I see these things-

JSF code -




HTML generated code -


JSF code -




HTML generated code -




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I see. So I shouldn't really consider JSF as a dialect of HTML. It doesn't seem to have the same escaping rules as HTML and XML. Have you tried <xsl:output method="text">? If that doesn't work then this may be a place where disable-output-escaping is required.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert &amp;amp; to &
 
Similar Threads
How to Add a Line Counter for Each XML Element in XSL?
XSLT value-of parameter not works as xml value
XSLT: escaping " in output text
xsl template question
An XSLT that's just a little more Specific