使用xslt更改xml结构

时间:2012-11-16 17:52:33

标签: xslt

我想使用xslt转换将以下源xml结构转换为目标xml结构。我可以使用xslt将以下源xml转换为目标xml。请帮助我们转发这个。

源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>

</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>
</<EmployeeCompetencyRequest>

3 个答案:

答案 0 :(得分:0)

一些提示:

<EmployeeCompetencyRequest>
 template match /XxhrPiEmpcompOutIntCollection
   for-each-group XxhrPiEmpcompOutInt group by employeeNumber
      <EmployeeNumber> value-of current-group-key </EmployeeNumber>
      <Competencies>
       for-each current-group
         <Competency>
            value-of name  
            value-of proficiencyLevel
         </competency>
      /for-each
     </Competencies>
  /for-each-group
 <EmployeeCompetencyRequest>

希望你能完成它。

答案 1 :(得分:0)

您可以使用for-each-group对用户进行分组:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
               exclude-result-prefixes="xs"
               version="2.0">
<xsl:output indent="yes"/>    

<xsl:template match="/">

    <EmployeeCompetencyRequest>
        <xsl:for-each-group select="//XxhrPiEmpcompOutInt" group-by="employeeNumber">
            <Name><xsl:value-of select="current-grouping-key()"/></Name>
            <Competencies>
            <xsl:for-each select="current-group()">   
                <Competency>
                    <Name><xsl:value-of select="competencyName"/></Name>
                    <ProfiencyLevel><xsl:value-of select="profiencyLevel"/></ProfiencyLevel>
                    <EndDate><xsl:value-of select="compDateTo"/></EndDate>
                </Competency>
            </xsl:for-each>
            </Competencies>
        </xsl:for-each-group>
    </EmployeeCompetencyRequest>

</xsl:template>   

萨吕,

答案 2 :(得分:0)

对于XSLT 1.0,您必须对width xsl:key:

进行分组
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
              version="1.0">
<xsl:output method="xml" indent="yes"/>      

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

<xsl:template match="/">
    <EmployeeCompetencyRequest>
    <xsl:apply-templates select="//XxhrPiEmpcompOutInt[generate-id() = generate-id(key('groups', employeeNumber)[1])]"/>
    </EmployeeCompetencyRequest>
</xsl:template>

<xsl:template match="XxhrPiEmpcompOutInt">
    <Name><xsl:value-of select="employeeNumber"/></Name>
    <Competencies>
        <xsl:for-each select="key('groups', employeeNumber)">
                <Competency>
                    <Name><xsl:value-of select="competencyName"/></Name>
                    <ProfiencyLevel><xsl:value-of select="proficiencyLevel"/></ProfiencyLevel>
                    <EndDate><xsl:value-of select="compDateTo"/></EndDate>
                </Competency>
            </xsl:for-each>
    </Competencies>
</xsl:template>    
</xsl:stylesheet>

我无法用Oracle测试它......