2012-11-16 149 views
-3

我想使用xslt轉換將以下源xml結構轉換爲目標xml結構。我很高興能夠使用xslt將以下源xml轉換爲目標xml。請幫助我們解決這個問題。使用xslt更改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> 
+2

歡迎來到SO。請向我們展示您已經寫好的代碼,告訴我們您的卡住的確切位置,然後很多人會盡力幫助您解決您的問題。 –

回答

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> 

希望你能完成它。

0

您可以將用戶與-各個羣組:

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

薩呂,

+0

感謝您的幫助utrescu。其實,這工作fie xslt 2.0,但我正在開發一個應用程序使用oracle sos 11g只支持xslt 1.o。我曾嘗試在xslt 1.0中使用以下方法執行此操作,但它不起作用。請幫幫我。 –

+0

我試過這個xslt 1.0,但它不工作。請幫助我 –

+0

在XSLT 1.0中,您可以使用xsl:key將其分組 – utrescu

0

對於XSLT 1.0,你必須組widht XSL:關鍵:

<?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測試它...