使用变量在循环中连接数据,然后在xslt外部使用它

时间:2017-04-25 10:00:25

标签: xslt foreach

我想将xml转换为mq消息格式。我尝试在foreach中使用变量,但由于变量在xslt中是静态的,因此无效。

输入xml是:

<ALCSHCALCSSpecialDays>
                    <ALCSHCALCSSpecialDay>
                       <Year>
                          <SpecifiedYear>2015</SpecifiedYear>
                          <NonSpecifiedYear/>
                       </Year>
                       <Month>
                          <SpecifiedMonth>6</SpecifiedMonth>
                          <NonSpecifiedMonth/>
                       </Month>
                       <DayOfMonth>
                          <SpecifiedDayOfMonth>16</SpecifiedDayOfMonth>
                          <LastDayOfMonth/>
                          <SecondLastDayOfMonth/>
                          <NonSpecifiedDayOfMonth/>
                       </DayOfMonth>
                       <DayOfWeek>
                          <SpecifiedDayOfWeek>4</SpecifiedDayOfWeek>
                          <NonSpecifiedDayOfWeek/>
                       </DayOfWeek>
                    </ALCSHCALCSSpecialDay>
                    <ALCSHCALCSSpecialDay>
                       <Year>
                          <SpecifiedYear>2015</SpecifiedYear>
                          <NonSpecifiedYear/>
                       </Year>
                       <Month>
                          <SpecifiedMonth>12</SpecifiedMonth>
                          <NonSpecifiedMonth/>
                       </Month>
                       <DayOfMonth>
                          <SpecifiedDayOfMonth>23</SpecifiedDayOfMonth>
                          <LastDayOfMonth/>
                          <SecondLastDayOfMonth/>
                          <NonSpecifiedDayOfMonth/>
                       </DayOfMonth>
                       <DayOfWeek>
                          <SpecifiedDayOfWeek>5</SpecifiedDayOfWeek>
                          <NonSpecifiedDayOfWeek/>
                       </DayOfWeek>
                    </ALCSHCALCSSpecialDay>
                 </ALCSHCALCSSpecialDays>

预期产量为: 16/6 / 2015,23 /二千零十五​​分之十二

以下是我尝试过的代码:

<xsl:variable name="dateOfSpecialDay"/>
<xsl:function name="f:concateDate">
        <xsl:param name="year"/>
        <xsl:param name="day"/>
        <xsl:param name="month"/>
        <xsl:variable name="dateOfSpecialDay">
                <xsl:value-of select="concat($day,'/',$month,'/',$year)"/>
            </xsl:variable>
        </xsl:function>   

 <xsl:for-each select="/eboebo:AsyncQueryLocationResponseEBM/eboebo:Custom/ns2:CustomLocationResponse/ns2:readInvResponse/ns2:readALCSData/ns2:ALCSHCALCSSpecialDays/ns2:ALCSHCALCSSpecialDay">

           <xsl:variable name="day" select="../ns2:ALCSHCALCSSpecialDays/ns2:ALCSHCALCSSpecialDay/ns2:DayOfMonth/ns2:SpecifiedDayOfMonth"/>
           <xsl:variable name="month" select="../ns2:ALCSHCALCSSpecialDays/ns2:ALCSHCALCSSpecialDay/ns2:Month/ns2:SpecifiedMonth"/>
           <xsl:variable name="year" select="../ns2:ALCSHCALCSSpecialDays/ns2:ALCSHCALCSSpecialDay/ns2:Year/ns2:SpecifiedYear"/>

            <xsl:value-of select="f:concateDate($year,$month,$day)"/>       
             </xsl:for-each>
<imp1:ResponseText>
            <xsl:value-of select='concat($dateOfSpecialDay,',')'/>
          </imp1:ResponseText>

我正在尝试执行空指针异常母鸡。

请任何人指导我完成此操作。 感谢。

1 个答案:

答案 0 :(得分:0)

问题是变量在XSLT中是“不可变的”,并且无法更改。在函数中设置变量的地方,实际上是在创建一个“阴影”全局变量的新变量。局部变量只存在于函数的范围内。它与全局变量不同。

你根本不需要这个变量。首先,只需更改函数即可返回连接日期

<xsl:function name="f:concateDate">
    <xsl:param name="year"/>
    <xsl:param name="day"/>
    <xsl:param name="month"/>
    <xsl:value-of select="concat($day,'/',$month,'/',$year)"/>
 </xsl:function> 

然后,更改您的xsl:for-each依次调用该函数,并输出值和逗号。例如

<imp1:ResponseText>
  <xsl:for-each select="//ns2:ALCSHCALCSSpecialDay">
       <xsl:variable name="day" select="ns2:DayOfMonth/ns2:SpecifiedDayOfMonth"/>
       <xsl:variable name="month" select="ns2:Month/ns2:SpecifiedMonth"/>
       <xsl:variable name="year" select="ns2:Year/ns2:SpecifiedYear"/>
       <xsl:if test="position() > 1">,</xsl:if>
       <xsl:value-of select="f:concateDate($year,$month,$day)"/>       
  </xsl:for-each>
</imp1:ResponseText>