将 XML 属性转换为有效的 HTML 属性值?
我在 Tomcat 6.0.33 应用程序服务器上使用 Java 6。我收到必须将其呈现为表单元素的 XML。我收到的 XML 如下所示
<pquotec type='input' label='Price Quote Currency' nwidth='200' vlength='10'>
XYZ
</pquotec>
,下面是所需的输出。
<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#" />
我的问题是,将 XML 元素的标签属性中存储的值转换为可以替换上面的“#valid_name_goes_here#”的值的策略是什么?最好该策略允许我再次翻译回来。请注意,“”内出现的内容不一定适合 id 和 name 的值。
谢谢你的帮助,-戴夫
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输入元素的 name 属性被定义为具有 CDATA 类型,这基本上意味着“任何字符数据”,所以我认为实际上应该不存在问题。
如果您确实遇到有效性问题,您可以将任何“尴尬”(或只是所有)字符转换为其编码形式。例如 é 将变成 é。
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 é.
使用 XSLT - 这是一个将 XML 转换为 HTML 的示例,但将 XML 转换为 XML 很简单以及。
在java中Xalan可以做XSLT,而此线程也可能对您有帮助。
如果您想使用 JSP 进行 XML 解析并呈现目标 HTML,请参阅此线程 获取 XML 解析器列表
编辑:
嗯,您可以在没有 XML & 的情况下编写问题。 HTML 片段,并简单询问如何将任何字符串转换为有效的 HTML Id,然后再转换回来。
使用
data-
属性 HTML 来存储原始传入字符串。然后使用正则表达式从传入字符串中提取有效字符,用下划线替换所有无效字符,并将其用作 ID。您获得重复 ID 的可能性很小。在这种情况下,您始终可以返回并使 XML 以不重复的方式出现。这样您就可以取回原始字符串并获得有效 ID
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