我需要根据源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>
答案 0 :(得分: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>
<强>解释强>:
正确使用 Muenchian Grouping Method 和 modes 。