用动态数据替换占位符

时间:2017-11-10 14:24:12

标签: java xslt placeholder

是否有使用XSL或java.text.MessageFormat实现以下功能的通用方法?

enter image description here

1 个答案:

答案 0 :(得分:1)

如果输入XML如下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>    
    <!--prepare message formatted template as below-->
    <xsl:template name="message">
        <xsl:param name="client.firstname"/>
        <xsl:param name="client.lastname"/>
        <xsl:param name="client.order.no"/>
        <xsl:param name="client.pickup.date"/>
        <xsl:param name="client.mobile.no"/>
        <xsl:variable name="var.h" select="concat('&#xa;', 'hi ', $client.firstname, ' ', $client.lastname, ',', '&#xa;', '', '&#xa;')"/>
        <xsl:variable name="var.b.str1" select="concat('Thanks for your order # is ', $client.order.no, '.', '&#xa;')"/>
        <xsl:variable name="var.b.str2" select="concat('We will send you updates on pick up by ', $client.pickup.date, ' on your mobile # ', $client.mobile.no, '.', '&#xa;')"/>
        <xsl:variable name="var.s" select="concat('', '&#xa;', 'Regards', '&#xa;', 'Team', '&#xa;')"/>           
        <Message>       
            <xsl:value-of select="concat($var.h, $var.b.str1, $var.b.str2, $var.s)"/>    
        </Message>        
    </xsl:template>
    <!--run you formatted message template by call-template function-->
    <xsl:template match="/Order">
        <xsl:call-template name="message">
            <xsl:with-param name="client.firstname" select="./Customer/@FirstName" />
            <xsl:with-param name="client.lastname" select="./Customer/@LastName" />
            <xsl:with-param name="client.order.no" select="./@OrderNo" />
            <xsl:with-param name="client.pickup.date" select="./OrderDates/OrderDate[@Type = 'PickDate' and @Name = 'PickUpdateDate']/@Value" />
            <!--below you can pass city code as below for mobile no-->
            <xsl:with-param name="client.mobile.no" select="concat('91-', ./Customer/@MobileNo)" />
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

然后使用如下的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<Message>
hi John Doe,

Thanks for your order # is T122345.
We will send you updates on pick up by 2017-11-10 10:00:00 on your mobile # 91-123456789.

Regards
Team
</Message>

您将达到预期的结果:

SELECT * FROM `analysis`
    INNER JOIN sample ON sample.id = analysis.sample_id
    INNER JOIN region ON sample.region_id = region.id
    INNER JOIN nature ON sample.nature_id = nature.id
    INNER JOIN garden ON sample.garden_id = garden.id
    WHERE sample.stateProduct = 'Origin'
    AND analysis.status = '0'
    AND region.name = 'Jangsu'
    AND garden.name = 'North Tukvar'
    AND nature.name = 'Thé Vert'
    AND sample.sampleBio = '1'
    AND sample.supplierCountry = 'Inde'