xml 转换 - 组合节点 [英] xml transformation - combining nodes
本文介绍了xml 转换 - 组合节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下测试用例的数据集
i have the following data set for a test case
<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS_LIST dateGenerated="2014-01-01">
<STUDENTS>
<MALE>
<STUDENT_ID>10000</STUDENT_ID>
<F_NAME>REGGIE</F_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/>
<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_C/>
</OPT_SUBJECTS>
<STUDENT_KEY>ABC10000-1</STUDENT_KEY>
<STUDENT_KEY_CREATION_DATE>04-20-2000</STUDENT_KEY_CREATION_DATE>
</MALE>
<FEMALE>
<STUDENT_ID>10001</STUDENT_ID>
<F_NAME>REGINA</F_NAME>
<M_NAME/>
<L_NAME>MOON</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/>
<FATHER_NAME>
<NAME>MOON A</NAME>
</FATHER_NAME>
<MOTHER_NAME>
<NAME>MOON 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_C/>
</OPT_SUBJECTS>
<STUDENT_KEY>ABC10000-1</STUDENT_KEY>
<STUDENT_KEY_CREATION_DATE>04-20-2000</STUDENT_KEY_CREATION_DATE>
</FEMALE>
</STUDENTS>
</STUDENTS_LIST>
需要使用xslt转换成如下xml
which needs to be converted into the following xml using xslt
<?xml version="1.0" encoding="UTF-8"?>
<classified date="2014-01-01">
<students>
<student>
<student_id>10000</student_id>
<fname>REGGIE</fname>
<mname/>
<lname>MILLER</lname>
<sex>male</sex>
<data>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 </data>
<dob>
<year>1980</year>
</dob>
<joindate>04-20-2000</joindate>
</student>
<student>
<student_id>10001</student_id>
<fname>REGINA</fname>
<mname/>
<lname>MOON</lname>
<sex>female</sex>
<notes>student_type: MORNING, student_ref: BLUE, father_name: MOON A,
mother_name: MOON 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>
<dob>
<year>1980</year>
</dob>
<joindate>04-20-2000</joindate>
</student>
</students>
</classified>
我使用@Joel M. Lamsen 提供的以下 XSLT 来回答我的其他问题之一.
I use the following XSLT provided by @Joel M. Lamsen in response to one of my other questions.
<?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>
</xsl:text>
</xsl:template>
<xsl:template match="NOTES"/>
<xsl:template match="REFRESH_DATE">
<xsl:value-of select="concat('REC_REFRESH_DATE', ': ', .)"/><xsl:text>
</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>
</xsl:text>
</xsl:template>
<xsl:template match="STUDENT">
<xsl:copy>
<xsl:apply-templates select="STUDENT_ID|F_NAME|M_NAME|L_NAME|NOTES|DOB"/>
<NOTES>
<xsl:text>
</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 converted needs to be in the exact mapping order and needs to be sorted on male/female basis.
在这方面的任何帮助将不胜感激.
any help in this regard would be appreciated.
前面的例子已经解决了.
a previous example has already been solved.
问候雷吉.
推荐答案
我已经改编了@helderdarocha 下面的样式表:
I have adapted the stylesheet below from @helderdarocha:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<classified date="{STUDENTS_LIST/@dateGenerated}">
<students>
<xsl:apply-templates select="STUDENTS_LIST/STUDENTS/MALE|STUDENTS_LIST/STUDENTS/FEMALE"/>
</students>
</classified>
</xsl:template>
<xsl:template match="*" mode="notes">
<xsl:value-of select="translate(name(.), $uppercase, $lowercase)"/>
<xsl:text>: </xsl:text>
<xsl:choose>
<xsl:when test="child::text()">
<xsl:value-of select="child::text()"/>
<xsl:if test="not(self::REFRESH_DATE)">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="*[.!='']">
<xsl:value-of select="."/>
<xsl:text>, </xsl:text>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="tag">
<xsl:element name="{translate(name(.), $uppercase, $lowercase)}">
<xsl:choose>
<xsl:when test="name() = 'DOB'">
<year><xsl:value-of select="."/></year>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="MALE|FEMALE">
<student>
<xsl:apply-templates select="STUDENT_ID|F_NAME|M_NAME|L_NAME" mode="tag"/>
<sex><xsl:value-of select="translate(name(), $uppercase, $lowercase)"/></sex>
<xsl:if test="*[not(STUDENT_ID|F_NAME|M_NAME|L_NAME|DOB|JOIN_DATE)]">
<notes>
<xsl:apply-templates select="STUDENT_TYPE|STUDENT_REF|FATHER_NAME|MOTHER_NAME|CORE_SUBJECTS|OPT_SUBJECTS|STUDENT_KEY|STUDENT_KEY_CREATION_DATE" mode="notes"/>
<xsl:apply-templates select="REFRESH_DATE" mode="notes"/>
</notes>
</xsl:if>
<xsl:apply-templates select="DOB|JOIN_DATE" mode="tag"/>
</student>
</xsl:template>
</xsl:stylesheet>
这篇关于xml 转换 - 组合节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文