简体   繁体   中英

XML to XML Transformation using XSLT - multiple nodes combined into 1

I have the following 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>

And I need to transform it into another XML (using XSLT), which gives the following output:

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

Since I have very little idea of XSLT transformation, any help with XSLT code would be highly appreciated.

Thanks.

try the following template:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM