0

我需要根据源 xml 中的employeeNumber 值分组将平面 xml 结构转换为嵌套 xml 结构。我在 xsl:key 的帮助下使用 xslt 1.0 进行了尝试,但它无法正常工作。请帮我解决问题。提前致谢。

输入xml

<XxhrPiEmpcompOutIntCollection>
          <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000562
            </employeeNumber>
            <competencyName>
                Comp1
            </competencyName>
            <proficiencyLevel>
                Prof1
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>

           <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000562
            </employeeNumber>
            <competencyName>
                Comp2
            </competencyName>
            <proficiencyLevel>
                Prof2
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>
          <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000574
            </employeeNumber>
            <competencyName>
                Comp2
            </competencyName>
            <proficiencyLevel>
                Prof2
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>

        </XxhrPiEmpcompOutIntCollection>

目标 xml

<EmployeeCompetencyRequest>
        <EmployeeNumber>200000562</EmployeeNumber>
        <Competencies>
            <Competency>
                <Name>Comp1</Name>
                <ProficiencyLevel>Prof1</ProficiencyLevel>
                <EndDate>16-NOV-12</EndDate>
            </Competency>
            <Competency>
                <Name>Comp2</Name>
                <ProficiencyLevel>Prof2</ProficiencyLevel>
                <EndDate>16-NOV-12</EndDate>
            </Competency>
        </Competencies>
        <EmployeeNumber>200000574</EmployeeNumber>
            <Competencies>
                <Competency>
                    <Name>Comp2</Name>
                    <ProficiencyLevel>Prof2</ProficiencyLevel>
                    <EndDate>16-NOV-12</EndDate>
                </Competency>
        </Competencies>
    </<EmployeeCompetencyRequest>
4

1 回答 1

0

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kOutIntByENum" match="XxhrPiEmpcompOutInt" use="employeeNumber"/>

 <xsl:template match="/*">
  <EmployeeCompetencyRequest><xsl:apply-templates/></EmployeeCompetencyRequest>
 </xsl:template>    

 <xsl:template match=
 "XxhrPiEmpcompOutInt
   [generate-id()
   =
    generate-id(key('kOutIntByENum', employeeNumber)[1])]">
  <EmployeeNumber>
    <xsl:value-of select="normalize-space(employeeNumber)"/>
  </EmployeeNumber>
  <Competencies>
    <xsl:apply-templates mode="comp"
         select="key('kOutIntByENum', employeeNumber)"/>
  </Competencies>
 </xsl:template>

 <xsl:template match="XxhrPiEmpcompOutInt" mode="comp">
  <Competency>
   <xsl:apply-templates mode="comp" select="*[not(self::employeeNumber)]"/>
  </Competency>
 </xsl:template>

 <xsl:template match="competencyName" mode="comp">
   <Name><xsl:value-of select="normalize-space(.)"/></Name>
 </xsl:template>
 <xsl:template match="proficiencyLevel" mode="comp">
   <ProficiencyLevel><xsl:value-of select="normalize-space(.)"/></ProficiencyLevel>
 </xsl:template>
 <xsl:template match="compDateTo" mode="comp">
   <EndDate><xsl:value-of select="normalize-space(.)"/></EndDate>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<XxhrPiEmpcompOutIntCollection>
          <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000562
            </employeeNumber>
            <competencyName>
                Comp1
            </competencyName>
            <proficiencyLevel>
                Prof1
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>

           <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000562
            </employeeNumber>
            <competencyName>
                Comp2
            </competencyName>
            <proficiencyLevel>
                Prof2
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>
          <XxhrPiEmpcompOutInt>
            <employeeNumber>
                200000574
            </employeeNumber>
            <competencyName>
                Comp2
            </competencyName>
            <proficiencyLevel>
                Prof2
            </proficiencyLevel>
            <compDateTo>
                16-NOV-12
            </compDateTo>
          </XxhrPiEmpcompOutInt>
</XxhrPiEmpcompOutIntCollection>

产生想要的正确结果:

<EmployeeCompetencyRequest>
   <EmployeeNumber>200000562</EmployeeNumber>
   <Competencies>
      <Competency>
         <Name>Comp1</Name>
         <ProficiencyLevel>Prof1</ProficiencyLevel>
         <EndDate>16-NOV-12</EndDate>
      </Competency>
      <Competency>
         <Name>Comp2</Name>
         <ProficiencyLevel>Prof2</ProficiencyLevel>
         <EndDate>16-NOV-12</EndDate>
      </Competency>
   </Competencies>
   <EmployeeNumber>200000574</EmployeeNumber>
   <Competencies>
      <Competency>
         <Name>Comp2</Name>
         <ProficiencyLevel>Prof2</ProficiencyLevel>
         <EndDate>16-NOV-12</EndDate>
      </Competency>
   </Competencies>
</EmployeeCompetencyRequest>

说明

正确使用孟池分组方法模式

于 2012-11-18T16:22:58.117 回答