用于将一个xml转换为另一个xml的XSLT代码

时间:2014-12-06 16:24:34

标签: xslt

你好Guyz我是XSLT的新手。请帮助创建XSLT以获得以下输出。

输入XML

<ProjectedMonthlyIncomeList>
  <ProjectedMonthlyIncome>
      <StartDate>2014-09-11T00:00:00-04:00</StartDate>
      <EndDate>2014-09-30T00:00:00-04:00</EndDate>
      <Month>Sep</Month>
      <Income>372.00</Income>
  </ProjectedMonthlyIncome>
  <ProjectedMonthlyIncome>
      <StartDate>2014-10-01T00:00:00-04:00</StartDate>
      <EndDate>2014-10-31T00:00:00-04:00</EndDate>
      <Month>Oct</Month>
      <Income>222.00</Income>
  </ProjectedMonthlyIncome>
  <ProjectedMonthlyIncome>
      <StartDate>2014-11-01T00:00:00-04:00</StartDate>
      <EndDate>2014-11-29T23:00:00-05:00</EndDate>
      <Month>Nov</Month>
      <Income>379.00</Income>
  </ProjectedMonthlyIncome>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:apply-templates match = "ProjectedMonthlyIncomeList/ProjectedMonthlyIncome">
        <xsl:with-param name="sequence" select="1" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="ProjectedMonthlyIncomeList/ProjectedMonthlyIncome">
    <xsl:param name="sequence" />
        <xsl:if test = "$sequence = '1'">
            <mon1><xsl:value-of select="Income"/></mon1>
        </xsl:if>
</xsl:template>
</xsl:stylesheet>

输出XML

<Mon1>372.00</Mon1>
<Mon2>222.00</Mon2>
<Mon3>379.00</Mon3>

我希望每个ProjectedMonthlyIncome标记将Income标记映射到Mon1,Mon2,Mon3

1 个答案:

答案 0 :(得分:0)

试试这个:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="ProjectedMonthlyIncomeList">
    <xsl:for-each select="ProjectedMonthlyIncome/Income">
        <xsl:element name="Mon{position()}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>