0

I'm using Java 6 on a Tomcat 6.0.33 application server. I'm getting XML that I must render as a form element. The XML I receive looks like

 <pquotec type='input' label='Price Quote Currency' nwidth='200' vlength='10'>
   XYZ
 </pquotec>

and below is the desired output.

 <label for="#valid_id_goes_here">Price Quote Currency</label>
 <input type="text" size="10" style="width:200px;" value="XYZ" name="#valid_name_goes_here#" id="#valid_id_goes_here#" />

My question is, what is a strategy for transforming the value stored in the XML element's label attribute to something I can replace "#valid_name_goes_here#" above with? Preferably the strategy would allow me to translate back again. Note that things that appear within "" may not necessarily be suitable for values for id and name.

Thanks for your help, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

1

The name attribute of the input element is defined as having type CDATA, which basically means "any character data", so I think there shouldn't really be a problem.

If you do encounter a validity issue, you could convert any 'awkward' (or simply all) characters to their encoded form. E.g. é would become &#233;.

Dabbler
  • 9,733
  • 5
  • 41
  • 64
  • Yeah but if the label is "Price & Currency", the expression id="Price & Currency" wouldn't fly. Also converting to id="Price & Currency" probably wouldn't fly either. I don't care what the id="" expression is, but I need it to be valid and I need to derive it from the label expression (and then back again). Does this make sense? – Dave Oct 11 '11 at 13:20
  • The question makes sense entirely. Your first post mentioned "#valid_name_goes_here#", so I was focusing on the name attribute ("#valid_id_goes_here#" was scrolled off to the right ;-). Now I see the id attribute is still a problem. Since the type of the id attribute is ID, which doesn't allow much more than A-Za-z and digits, you will need to roll your own encoding. A simple way would be to replace each character by its Unicode codepoint in hex. The id also has to start with a letter. Thus, "XYZ" would become "id00580059005A". That's unambiguous and also supports roundtripping. – Dabbler Oct 11 '11 at 14:30
0

USE XSLT - Heres an example that converts XML to HTML, but it is trivial to convert XML to XML as well.

In java Xalan can do XSLT, and this thread might also help you.

In case you want to do the XML Parsing and render the target HTML using JSP refer to this thread for a list of XML Parsers

EDIT:

Hmmm, you could have written the question without XML & HTML fragments, and asked simply how to convert any string into a valid HTML Id, and back again.

Use the data- attributes HTML to store the original incoming string. Then use regex to extract valid characters from the incoming string, replacing all invalid characters with underscore, and use that as ID. There is a small chance that you may get duplicate IDs. In that case you can always go back and make the XML come in a way that does not have duplicates.

This way you can get back the original string and have the Valid IDs

Community
  • 1
  • 1
Zasz
  • 12,330
  • 9
  • 43
  • 63
  • Hi, Actually my question is seeking an algorithm to convert the label, like "Price Quote Currency" to a valid value for the HTML "id" attribute. Obviously, "Price Quote Currency" by itself won't work because spaces aren't valid in the id field. Do you understand what I'm asking? – Dave Oct 10 '11 at 20:02