1

我想在 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>
4

1 回答 1

1

使用模板可以简化 XSLT,而不是使用<xsl:for-each>. 由于输出中保留了很多节点,因此您可以使用identity template开始。

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

然后<STUDENTADD>将包含所有节点<STUDENTS>,可以通过以下方式完成

<xsl:template match="INPUT">
    <CLASSES>
        <xsl:apply-templates select="POST/CLASSES/CLASS" />
        <STUDENTS>
            <xsl:apply-templates select="POST/CLASSES/STUDENTS/STUDENTADD" />
            <xsl:apply-templates select="PRE/CLASSES/STUDENTS/STUDENTADD" />
        </STUDENTS>
    </CLASSES>
</xsl:template>

最后,将添加一个新节点<CHANGE_CODE>作为<STUDENTADD>. 这里的假设是,如果 的祖先<STUDENTADD>恰好是 ,<POST>那么 的值就是<CHANGE_CODE>123如果是 ,<PRE>那么 的值就是124

<xsl:template match="STUDENTADD">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="ancestor::POST">
                <CHANGE_CODE>123</CHANGE_CODE>
            </xsl:when>
            <xsl:when test="ancestor::PRE">
                <CHANGE_CODE>124</CHANGE_CODE>
            </xsl:when>
        </xsl:choose>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

下面是完整的 XSLT 和输出。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="INPUT">
        <CLASSES>
            <xsl:apply-templates select="POST/CLASSES/CLASS" />
            <STUDENTS>
                <xsl:apply-templates select="POST/CLASSES/STUDENTS/STUDENTADD" />
                <xsl:apply-templates select="PRE/CLASSES/STUDENTS/STUDENTADD" />
            </STUDENTS>
        </CLASSES>
    </xsl:template>

    <xsl:template match="STUDENTADD">
        <xsl:copy>
            <xsl:choose>
                <xsl:when test="ancestor::POST">
                    <CHANGE_CODE>123</CHANGE_CODE>
                </xsl:when>
                <xsl:when test="ancestor::PRE">
                    <CHANGE_CODE>124</CHANGE_CODE>
                </xsl:when>
            </xsl:choose>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<CLASSES>
    <CLASS>
        <CLASSNAME>SIXTH</CLASSNAME>
        <NOOFSTUDENT>60</NOOFSTUDENT>
    </CLASS>
    <STUDENTS>
        <STUDENTADD>
            <CHANGE_CODE>123</CHANGE_CODE>
            <ID>1</ID>
            <ADDRESS>
                <POST_TOWN>123456</POST_TOWN>
                <POST_CODE>56859542</POST_CODE>
            </ADDRESS>
            <STARTDATE>29-12-2015</STARTDATE>
        </STUDENTADD>
        <STUDENTADD>
            <CHANGE_CODE>123</CHANGE_CODE>
            <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>
        <STUDENTADD>
            <CHANGE_CODE>124</CHANGE_CODE>
            <ID>1</ID>
            <ADDRESS>
                <POST_TOWN>12345692</POST_TOWN>
                <POST_CODE>56859542</POST_CODE>
            </ADDRESS>
            <STARTDATE>2015-12-29</STARTDATE>
        </STUDENTADD>
    </STUDENTS>
</CLASSES>
于 2018-04-12T13:08:04.767 回答