使用变量在循环中连接数据,然后在xslt外部使用它
我想将xml转换为mq消息格式。我尝试在foreach中使用变量,但它不起作用,因为变量在xslt中是静态的 输入xml为:使用变量在循环中连接数据,然后在xslt外部使用它,xslt,foreach,Xslt,Foreach,我想将xml转换为mq消息格式。我尝试在foreach中使用变量,但它不起作用,因为变量在xslt中是静态的 输入xml为: <ALCSHCALCSSpecialDays> <ALCSHCALCSSpecialDay> <Year> <SpecifiedYear>2015</SpecifiedYear>
<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>
2015
6.
16
4.
2015
12
23
5.
预期产出为:
2015年6月16日,2015年12月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>
当我试图执行时,我得到空指针异常
请任何人都能指导我完成这件事。
谢谢。问题在于变量在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>
谢谢你的回复。我会试试这个。
<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>