gpt4 book ai didi

java - 用动态数据替换占位符

转载 作者:行者123 更新时间:2023-12-02 12:01:29 25 4
gpt4 key购买 nike

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

enter image description here

最佳答案

如果您输入的 XML 如下:

<Order OrderNo="T122345">
<Customer FirstName="John" LastName="Doe" MobileNo="123456789"/>
<OrderDates>
<OrderDate Type="PickDate" Name="PickUpdateDate" Value="2017-11-10 10:00:00" />
</OrderDates>
</Order>

然后使用 XSL,如下所示:

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

您将达到预期的结果:

<?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>

关于java - 用动态数据替换占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47224920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com