开发者

Transforming an XML attribute into a valid HTML attribute value?

开发者 https://www.devze.com 2023-04-11 20:34 出处:网络
I\'m using 开发者_如何学CJava 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

I'm using 开发者_如何学CJava 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


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;.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号