我面临的问题要划分付款历史字符串
"Apr:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|"
至xml元素以下
<Periods>
<Period Year=2013>
<Months month1="Apr" month1Value="XXX/STD" month2="Jan" month2Value="XXX/STD"
</Period >
<Period Year=2012>
<Months month1="Dec" month1Value="XXX/STD" month2="Nov" month2Value="XXX/STD"
</Period>
</Periods>
来自注释
让我假设我的确切付款历史记录如下 " 4月:2013年,XXX/STD | 3月:2013年,XXX/STD | 2月:2013年,XXX/STD | JAN:2013,XXX/STD | DEC:2012,XXX/STD | 2012,XXX/STD |:2012,XXX/STD | SEP:2012,XXX/STD | 8月:2012年,XXX/STD | Jul:2012,XXX/STD | Jun:2012,XXX/STD | 5月:2012,XXX/STD | APR:2012,xxx/std | 3月:2012年,xxx/std |" 但是现在它产生了重复节点。请建议
假设您的字符串将包含按 Year 的付款历史记录。。
以下代码有什么作用:
1。它将每一个定界符'|'
将字符串拆分例如。它将生成
<temp>Apr:2013,XXX/STD|Jan:2013,XXX/STD</temp>
<temp>Dec:2012,XXX/STD|Nov:2012,XXX/STD</temp>
2。此外,它将通过存储在变量values
中的生成数组中获取每个<temp>
的值。
您去这里:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" version="1.0"
exclude-result-prefixes="exsl">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="payment_history">
<xsl:variable name="values">
<xsl:call-template name="split">
<xsl:with-param name="pText" select="."/>
</xsl:call-template>
</xsl:variable>
<Periods>
<xsl:for-each select="exsl:node-set($values)/*">
<Period Year="{substring-before(substring-after(normalize-space(.),':'),',')}">
<Months month1="{substring-before(.,':')}"
month1Value="{substring-before(substring-after(normalize-space(.),','),'|')}"
month2="{substring-before(substring-after(normalize-space(.),'|'),':')}"
month2Value="{substring-after(substring-after(normalize-space(.),'|'),',')}" />
</Period>
</xsl:for-each>
</Periods>
</xsl:template>
<xsl:template match="text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="normalize-space($pText)">
<xsl:variable name="vText" select="normalize-space($pText)"/>
<temp>
<xsl:value-of select="substring-before($vText, '|')"/>
<xsl:value-of select="concat('|', substring-before(substring-after($vText, '|'), '|'))"/>
</temp>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after(substring-after($vText, '|'), '|')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
http://xsltfiddle.liberty-development.net/bnnzw3
以这种方式尝试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:param name="input"/>
<xsl:template match="/">
<Periods>
<xsl:call-template name="tokenize-to-pairs">
<xsl:with-param name="text" select="$input"/>
</xsl:call-template>
</Periods>
</xsl:template>
<xsl:template name="tokenize-to-pairs">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:if test="contains($text, $delimiter) and contains(substring-after($text, $delimiter), $delimiter)">
<xsl:call-template name="parse-period">
<xsl:with-param name="month1" select="substring-before($text, $delimiter)"/>
<xsl:with-param name="month2" select="substring-before(substring-after($text, $delimiter), $delimiter)"/>
</xsl:call-template>
<!-- recursive call -->
<xsl:call-template name="tokenize-to-pairs">
<xsl:with-param name="text" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="parse-period">
<xsl:param name="month1"/>
<xsl:param name="month2"/>
<Period Year="{substring-before(substring-after($month1, ':'), ',')}">
<Months
month1="{substring-before($month1, ':')}"
month1Value="{substring-after($month1, ',')}"
month2="{substring-before($month2, ':')}"
month2Value="{substring-after($month2, ',')}"/>
</Period >
</xsl:template>
</xsl:stylesheet>
使用input
参数调用此样式表格:
Apr:2013,XXX/STD|Mar:2013,XXX/STD|Feb:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|Oct:2012,XXX/STD|Sep:2012,XXX/STD|Aug:2012,XXX/STD|Jul:2012,XXX/STD|Jun:2012,XXX/STD|May:2012,XXX/STD|Apr:2012,XXX/STD|Mar:2012,XXX/STD|
结果将是:
<Periods>
<Period Year="2013">
<Months month1="Apr" month1Value="XXX/STD" month2="Mar" month2Value="XXX/STD"/>
</Period>
<Period Year="2013">
<Months month1="Feb" month1Value="XXX/STD" month2="Jan" month2Value="XXX/STD"/>
</Period>
<Period Year="2012">
<Months month1="Dec" month1Value="XXX/STD" month2="Nov" month2Value="XXX/STD"/>
</Period>
<Period Year="2012">
<Months month1="Oct" month1Value="XXX/STD" month2="Sep" month2Value="XXX/STD"/>
</Period>
<Period Year="2012">
<Months month1="Aug" month1Value="XXX/STD" month2="Jul" month2Value="XXX/STD"/>
</Period>
<Period Year="2012">
<Months month1="Jun" month1Value="XXX/STD" month2="May" month2Value="XXX/STD"/>
</Period>
<Period Year="2012">
<Months month1="Apr" month1Value="XXX/STD" month2="Mar" month2Value="XXX/STD"/>
</Period>
</Periods>
请注意,这一年是第一个月的一年。