如何使用xslt 1.0在xml元素中分割字符串

时间:2019-03-19 09:35:16

标签: xml xslt xslt-1.0

我在划分付款历史字符串(如

)时遇到问题
"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>

来自评论

  

让我们假设我的确切付款记录如下   “ 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 |“   但现在它生成重复节点。请建议

2 个答案:

答案 0 :(得分:0)

假设您的字符串将包含按 year 分类的付款历史记录。

下面的代码做什么:

1。。它将每隔一个定界符'|'

分割字符串

例如它将生成

<temp>Apr:2013,XXX/STD|Jan:2013,XXX/STD</temp>
<temp>Dec:2012,XXX/STD|Nov:2012,XXX/STD</temp>

2。。此外,它将遍历存储在变量<temp>中的所生成数组中的每个values迭代中的值。

您在这里:

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

答案 1 :(得分:0)

尝试一下:

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>

请注意,期间的年份是第一个月的年份。