使用XSLT进行XML到XML转换 - 多个节点合并为1个

时间:2014-04-14 07:42:49

标签: xml xslt

我有以下XML:

<STUDENT_LIST>
   <STUDENTS>
      <MALE>
         <STUDENT_ID>10000</STUDENT_ID>
         <F_NAME>REGGIE</F_NAME>
         <M_NAME></M_NAME>
         <L_NAME>MILLER</L_NAME>
         <DOB>
            <YEAR>1980</YEAR>
         </DOB>
         <STUDENT_TYPE>MORNING</STUDENT_TYPE>
         <STUDENT_REF>BLUE</STUDENT_REF>
         <JOIN_DATE>04-20-2000</JOIN_DATE>
         <NOTES></NOTES>
         <FATHER_NAME>
            <NAME>MILLER A</NAME>
         </FATHER_NAME>
         <MOTHER_NAME>
            <NAME>MILLER B</NAME>
         </MOTHER_NAME>
         <REFRESH_DATE>04-14-2014</REFRESH_DATE>
         <CORE_SUBJECTS>
            <SUBJECT_A>CALCULUS A</SUBJECT_A>
            <SUBJECT_B>CALCULUS B</SUBJECT_B>
            <SUBJECT_C>PERFORMING ARTS</SUBJECT_C>
         </CORE_SUBJECTS>
         <OPT_SUBJECTS>
            <SUBJECT_A>AMERICAN HISTORY</SUBJECT_A>
            <SUBJECT_B></SUBJECT_B>
            <SUBJECT_C></SUBJECT_C>
         </OPT_SUBJECTS>
         <STUDENT_KEY>ABC10000-1</STUDENT_KEY>
         <STUDENT_KEY_CREATION_DATE>04-20-2000</STUDENT_KEY_CREATION_DATE>
      </MALE>
   </STUDENTS>
</STUDENT_LIST>

我需要将其转换为另一个XML(使用XSLT),它提供以下输出:

<STUDENT_LIST>
   <STUDENTS>
      <MALE>
         <STUDENT_ID>10000</STUDENT_ID>
         <F_NAME>REGGIE</F_NAME>
         <M_NAME/>
         <L_NAME>MILLER</L_NAME>
         <DOB>
            <YEAR>1980</YEAR>
         </DOB>
         <JOIN_DATE>04-20-2000</JOIN_DATE>
         <NOTES> 
            STUDENT_TYPE: MORNING
            STUDENT_REF: BLUE
            FATHER_NAME: MILLER A
            MOTHER_NAME: MILLER B
            CORE_SUBJECTS: CALCULUS A, CALCULUS B, PERFORMING ARTS
            OPT_SUBJECTS: AMERICAN HISTORY
            STUDENT_KEY: ABC10000-1
            STUDENT_KEY_CREATION_DATE: 04-20-2000
            REC_REFRESH_DATE: 04-14-2014
         </NOTES>
      </MALE>
   </STUDENTS>
</STUDENT_LIST>

由于我对XSLT转换知之甚少,所以对XSLT代码的任何帮助都将受到高度赞赏。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试以下模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="STUDENT_TYPE|STUDENT_REF|FATHER_NAME|MOTHER_NAME|STUDENT_KEY|STUDENT_KEY_CREATION_DATE">
        <xsl:value-of select="concat(name(), ': ', .)"/><xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="NOTES"/>

    <xsl:template match="REFRESH_DATE">
        <xsl:value-of select="concat('REC_REFRESH_DATE', ': ', .)"/><xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="CORE_SUBJECTS|OPT_SUBJECTS">
        <xsl:value-of select="concat(name(), ': ')"/>
        <xsl:for-each select="*[.!='']">
            <xsl:choose>
                <xsl:when test="position() != last()">
                    <xsl:value-of select="."/>
                    <xsl:text>, </xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="MALE">
        <xsl:copy>
            <xsl:apply-templates select="STUDENT_ID|F_NAME|M_NAME|L_NAME|DOB"/>
            <NOTES>
                <xsl:text>&#xA;</xsl:text>
                <xsl:apply-templates select="STUDENT_TYPE"/>
                <xsl:apply-templates select="STUDENT_REF"/>
                <xsl:apply-templates select="FATHER_NAME"/>
                <xsl:apply-templates select="MOTHER_NAME"/>
                <xsl:apply-templates select="CORE_SUBJECTS"/>
                <xsl:apply-templates select="OPT_SUBJECTS"/>
                <xsl:apply-templates select="STUDENT_KEY"/>
                <xsl:apply-templates select="STUDENT_KEY_CREATION_DATE"/>
                <xsl:apply-templates select="REFRESH_DATE"/>
            </NOTES>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>