使用XSLT将类似节点移动到新节点

时间:2016-08-12 09:32:29

标签: xslt

我有以下输入xml:

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

你能帮帮我吗?

1 个答案:

答案 0 :(得分: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>