字符串拼接
<xsl:attribute name="j_contact">
<xsl:value-of select="normalize-space(concat(sender_info/first_name, ' ',sender_info/last_name))"/>
</xsl:attribute>
格式化数字类型
#####0.## 表示整数部分6位,小数部分2位
个位是0是因为用#号情况下不会自动补0,假设重量是300,除以1000后会得到0.3
如果个位也是#,则得到的是.3
"parcelTotalWeight": "<xsl:value-of select="format-number(package_info/weight div 1000,'#####0.##')"/>"
判断元素值是否非空
<xsl:if test="package_info/length and package_info/length != ''">
"parcelTotalLength": "<xsl:value-of select="package_info/length"/>",
</xsl:if>
<xsl:choose>
<xsl:when test="not(shipping_info/phone) or shipping_info/phone = '' or shipping_info/phone = ' '">
<xsl:attribute name="d_tel"><xsl:value-of select="shipping_info/mobile"/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="d_tel"><xsl:value-of select="shipping_info/phone"/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
声明输出普通text,数字类型如果为空输出空字符串
<xsl:output method="text"/>
<xsl:decimal-format NaN=""/>
<xsl:template match="/order">
声明输出XML&外部参数
如此生成的XML顶部会有<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="xml" encoding="utf-8"/>
<xsl:param name="tiktok.routemap"/>
判断元素对象存在
<xsl:if test="boolean(//pickup_info)">
<xsl:choose>
<xsl:when test="pickup_info/pickup_type = '2'">
"pickupType": "0",
</xsl:when>
<xsl:when test="pickup_info/pickup_type = '1'">
"pickupType": "1",
</xsl:when>
</xsl:choose>
</xsl:if>
字符串长度
<xsl:when test="string-length(shipping_info/address/postcode) = 0 and $dd_country_code = 'CN'">
<xsl:attribute name="d_post_code">000000</xsl:attribute>
</xsl:when>
集合长度&遍历
<xsl:variable name="count" select="count(BigBagOrder/package_list/tracking_no)"/>
"package_list": [
<xsl:for-each select="package_list">"
<xsl:value-of select="tracking_no"/>"
<xsl:if test="position() < $count">,</xsl:if>
</xsl:for-each>]
字符串去空格&裁剪
normalize-space() 是 XSLT 和 XPath 中的一个函数,用于处理字符串。这个函数的主要目的是删除字符串中的前导和尾随空格,并将字符串中的连续空格序列替换为单个空格。
substring() 函数被用来从标准化后的字符串中取出子字符串。这里,它从第1个字符开始取,长度最多为60个字符。
<xsl:value-of select="substring(normalize-space(@stname),1,60)"/>
集合-任一属性为true
多个包裹有一个带电则判断为带电
<xsl:choose>
<xsl:when test="items[is_electricity='true']">
<xsl:attribute name="isBat">1</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="isBat">0</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
集合-计算总价值
多个包裹(单价*数量)的总和
<xsl:attribute name="declared_value">
<xsl:value-of select="sum(items/(unit_price * quantity))" />
</xsl:attribute>
时间时区处理
<xsl:choose>
<xsl:when test="contains($req_timezone, ':') and string-length($req_timezone) > 4"><!-- -09:30/+12:45 -->
<xsl:variable name="gmt_flag" select="substring($req_timezone,1,1)"/><!-- -/+ -->
<xsl:variable name="gmt_count1" select="substring-before($req_timezone, ':')"/><!-- -09 -->
<xsl:variable name="gmt_count2" select="concat($gmt_flag, number(substring-after($req_timezone, ':')) div 60)"/><!-- 30 -->
<xsl:variable name="timestamp1"><xsl:value-of select="$pickup_start_time + (number($gmt_count1) + number($gmt_count2)) * 60 * 60 * 1000"/></xsl:variable>
<xsl:variable name="req_data1" select="xs:dateTime('1970-01-01T00:00:00Z') + $timestamp1 * xs:dayTimeDuration('PT0.001S')"/>
<xsl:variable name="req_data2" select="format-dateTime(xs:dateTime($req_data1),'[Y0001]-[M01]-[D01] [H01]:[m01]:[s01]')"/>
<xsl:attribute name="appointment_time"><xsl:value-of select="$req_data2"/></xsl:attribute>
</xsl:when>
<xsl:when test="string-length($req_timezone) > 1"><!-- +8 -->
<xsl:variable name="timestamp1"><xsl:value-of select="$pickup_start_time + number($req_timezone) * 60 * 60 * 1000"/></xsl:variable>
<xsl:variable name="req_data1" select="xs:dateTime('1970-01-01T00:00:00Z') + $timestamp1 * xs:dayTimeDuration('PT0.001S')"/>
<xsl:variable name="req_data2" select="format-dateTime(xs:dateTime($req_data1),'[Y0001]-[M01]-[D01] [H01]:[m01]:[s01]')"/>
<xsl:attribute name="appointment_time"><xsl:value-of select="$req_data2"/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="appointment_time"><xsl:value-of select="$pickup_start_time"/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>