开发者

An alternative to XML that also supports expressions/methods

开发者 https://www.devze.com 2023-03-24 05:48 出处:网络
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

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 开发者_如何学Pythonlanguage 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?


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/


Have a look at Latex[wiki].


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.


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.

0

精彩评论

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

关注公众号