2016-08-12 110 views
0

我有以下輸入XML:移動類似的節點到新節點使用XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <PQGetCareGaps> 
     <METHOD>GET</METHOD> 
     <contract>HXXXX</contract> 
    </PQGetCareGaps> 
    <FinalCareGapResults> 
     <Gap> 
     <CareGap>Colorectal Cancer Screening</CareGap> 
     <GapHistory> 
      <row> 
       <MEMBERID>AAAAAA000016-00</MEMBERID> 
      </row> 
     </GapHistory> 
     </Gap> 
    </FinalCareGapResults> 
    <FinalCareGapResults> 
     <Gap> 
     <CareGap>Adult BMI Assessment</CareGap> 
     </Gap> 
    </FinalCareGapResults> 
</response> 

我想修改上面的XML以這樣的方式,所有的<Gap>節點應該來一個名爲<TestResults>新的節點下。由此產生的xml應該如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <PQGetCareGaps> 
     <METHOD>GET</METHOD> 
     <contract>HXXXX</contract> 
    </PQGetCareGaps> 
    <TestResults> 
     <Gap> 
     <CareGap>Colorectal Cancer Screening</CareGap> 
     <GapHistory> 
      <row> 
       <MEMBERID>AAAAAA000016-00</MEMBERID> 
      </row> 
     </GapHistory> 
     </Gap> 
     <Gap> 
     <CareGap>Adult BMI Assessment</CareGap> 
     </Gap> 
    </TestResults> 
</response> 

你能幫我嗎?

回答

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="1.0"> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="response" /> 
    </xsl:template> 
    <xsl:template match="response"> 
     <response> 
     <xsl:copy-of select="PQGetCareGaps" /> 
     <TestResults> 
     <xsl:for-each select="FinalCareGapResults/Gap"> 
      <xsl:copy-of select="." />   
     </xsl:for-each> 
     </TestResults> 
     </response> 
    </xsl:template> 
</xsl:stylesheet>