XML 的替代方案,也支持表达式/方法
我们有一个有趣的问题。我们是一家打印公司,允许用户打印基于各种第三方来源的模板材料。一个例子是查找待售房屋的详细信息并将这些值注入该房屋的宣传册中。
目前我们的模板是在 xml 文件中定义的。文本标签的一个属性是在我们的系统中调用的方法名称,以获取所需的自动填充值。由于后端是用 Java 编写的,我们使用反射来查找用户传入的几个对象之一中的方法。作为一个例子,它看起来像这样...
<PagePart>
<profileTag>**getAddress**</profileTag>
<text>123 Example St NW</text>
<fontFile>Helvetica.TTF</fontFile>
<fontName>Helvetica</fontName>
<fontStyle>0</fontStyle>
<fontAlignment>0</fontAlignment>
<fontSize>18</fontSize>
<positionLeft>19</positionLeft>
<positionTop>88</positionTop>
<Color>
<r>0</r>
<g>130</g>
<b>200</b>
</Color>
<visibility>false</visibility>
</PagePart>
profileTag 中的 getAddress 是我们系统中在初始化模板的新实例时调用的方法。当我们添加新模板时,我们需要像这样构建精美的字符串
public String getBusinessPhoneWithTitle() {
if (!phone.isEmpty()) return "B " + phone;
else return "";
}
。我们计划拥有数千个模板(目前为数百个),而这些仅支持一两个模板的“格式化”方法正在失控。我们需要一种方法,让元素通过我们发明或找到的脚本语言来包含实际逻辑。
我想知道是否有人知道一种脚本语言,它具有对象文字表示法,可以替换标记的 XML,同时提供可评估的代码。如果没有,您会使用哪种语言来评估动态表达式?
We have an interesting problem. We are a printing company that allows users to print templated material based on various 3rd party sources. An example would be looking up the details of a house for sale and injecting those values into a brochure for that house.
Currently our templates are defined in an xml file. One attribute of a label of text is the method name to call in our system to get the autopopulated value needed. As the backend is written in Java we use reflection to lookup the method in one of a few objects passed in by the user. As an example it looks like this...
<PagePart>
<profileTag>**getAddress**</profileTag>
<text>123 Example St NW</text>
<fontFile>Helvetica.TTF</fontFile>
<fontName>Helvetica</fontName>
<fontStyle>0</fontStyle>
<fontAlignment>0</fontAlignment>
<fontSize>18</fontSize>
<positionLeft>19</positionLeft>
<positionTop>88</positionTop>
<Color>
<r>0</r>
<g>130</g>
<b>200</b>
</Color>
<visibility>false</visibility>
</PagePart>
getAddress inside profileTag is the method in our system to call when initializing a new instance of a template. As we add new templates we have needs for fancy strings to be built up like this
public String getBusinessPhoneWithTitle() {
if (!phone.isEmpty()) return "B " + phone;
else return "";
}
We plan to have thousands of templates (currently hundreds) and these "formatting" methods that exist only to support one or two templates are getting out of control. We need a way for the element to contain actual logic via a scripting language we invent or one we find.
What I am wondering is if anybody knows of a scripting language that has an object literal notation that can replace XML for the markup while providing evaluable code at the same time. If not, what language would you smash into the element to evaluate dynamic expressions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将 Velocity Templates 与 java 一起使用来构造 xml。您可以将纯 java api 直接构建到您的应用程序中。主站点是:http://velocity.apache.org/
You could use Velocity Templates with your java to construct xml. There is a Pure java api that you can build right into your app. Home site is: http://velocity.apache.org/
看看 Latex[wiki]。
Have a look at Latex[wiki].
您是否考虑过使用 XPath 作为脚本语言?我见过一些应用程序非常成功地使用像您这样的 XML 结构作为固定部分,然后为需要计算的内容(例如,利率或汇率)嵌入 XPath 表达式。您可以通过在获取 XPath 表达式时对其求值来进行处理(从 Java 或使用 evaluate() 扩展从 XSLT 中求值),也可以将整个文档转换为 XSLT 样式表,然后由该样式表有效地求值自身。
Have you thought about using XPath as the scripting language? I've seen applications that very successfully use an XML structure like yours for the fixed part, and then embed XPath expressions for things that need to be computed (for example, interest rates or exchange rates). You can do the processing either by evaluating the XPath expressions as you get to them (from Java or from XSLT using an evaluate() extension), or you can transform the whole document into an XSLT stylesheet which then effectively evaluates itself.
您是否计划根据模板发布不同格式的文档? Quark Express 似乎是您更好的选择。我知道印刷行业的很多人都使用这个。
Do you plan to publish documents of varying formats based on the template ? Quark express seems like a better alternative for you. I know many folks in the printing business that use this.