-3

我想使用 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>
4

3 回答 3

0

对于 XSLT 1.0,您必须分组 widht 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 对其进行测试...

于 2012-11-18T16:30:52.803 回答
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>   

致敬,

于 2012-11-16T18:42:01.210 回答
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>

希望你能完成它。

于 2012-11-16T18:32:37.923 回答