我想在 xml 中间插入一些元素并删除 XML 中的一些元素。
请在下面找到用于转换 xml 的 XSLT。
<xsl:template match="/">
<xsl:element name="CLASSES">
<xsl:copy-of select="INPUT/POST/CLASSES/CLASS" />
<xsl:variable name="previous_id">
<xsl:value-of select="INPUT/PRE/CLASSES/STUDENTS/STUDENTADD/ID"></xsl:value-of>
</xsl:variable>
<xsl:element name="STUDENTS">
<xsl:for-each select="INPUT/POST/CLASSES/STUDENTS/STUDENTADD">
<xsl:choose>
<xsl:when test="$previous_id = ID">
<xsl:element name="change_code">
<xsl:value-of select="123" />
</xsl:element>
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:for-each select="INPUT/PRE/CLASSES/STUDENTS/STUDENTADD">
<xsl:element name="change_code">
<xsl:value-of select="124" />
</xsl:element>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
我得到如下带有插入元素的输出。
<CLASSES>
<CLASS>
<CLASSNAME>SIXTH</CLASSNAME>
<NOOFSTUDENT>60</NOOFSTUDENT>
</CLASS>
<STUDENTS>
<change_code>123</change_code>
<STUDENTADD>
<ID>1</ID>
<ADDRESS>
<POST_TOWN>123456</POST_TOWN>
<POST_CODE>56859542</POST_CODE>
</ADDRESS>
<STARTDATE>29-12-2015</STARTDATE>
</STUDENTADD>
<STUDENTADD>
<ID>2</ID>
<ADDRESS>
<POST_TOWN>W4589652</POST_TOWN>
<POST_CODE>51896</POST_CODE>
</ADDRESS>
<STARTDATE>25-12-2016</STARTDATE>
<END_DATE>25-12-2016</END_DATE>
</STUDENTADD>
<change_code>124</change_code>
<STUDENTADD>
<ID>1</ID>
<ADDRESS>
<POST_TOWN>12345692</POST_TOWN>
<POST_CODE>56859542</POST_CODE>
</ADDRESS>
<STARTDATE>2015-12-29</STARTDATE>
</STUDENTADD>
</STUDENTS>
</CLASSES>
我想要的是 change_code 元素应该在 STUdENT_ADD 聚合中。
我应该如何更改 xslt?
输入 XML 如下。
<INPUT>
<POST>
<CLASSES>
<CLASS>
<CLASSNAME>SIXTH</CLASSNAME>
<NOOFSTUDENT>60</NOOFSTUDENT>
</CLASS>
<STUDENTS>
<STUDENTADD>
<ID>1</ID>
<ADDRESS>
<POST_TOWN>123456</POST_TOWN>
<POST_CODE>56859542</POST_CODE>
</ADDRESS>
<STARTDATE>29-12-2015</STARTDATE>
</STUDENTADD>
<STUDENTADD>
<ID>2</ID>
<ADDRESS>
<POST_TOWN>W4589652</POST_TOWN>
<POST_CODE>51896</POST_CODE>
</ADDRESS>
<STARTDATE>25-12-2016</STARTDATE>
<END_DATE>25-12-2016</END_DATE>
</STUDENTADD>
</STUDENTS>
</CLASSES>
</POST>
<PRE>
<CLASSES>
<STUDENTS>
<STUDENTADD>
<ID>1</ID>
<ADDRESS>
<POST_TOWN>12345692</POST_TOWN>
<POST_CODE>56859542</POST_CODE>
</ADDRESS>
<STARTDATE>2015-12-29</STARTDATE>
</STUDENTADD>
</STUDENTS>
</CLASSES>
</PRE>
</INPUT>