使用XSLT xsl在XML中插入元素:for-each

时间:2018-04-12 12:35:05

标签: xml xslt updatexml

我想在xml的中间插入一些元素,并删除XML中的一些元素。

请在下面的XSLT中找到转换xml。

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

1 个答案:

答案 0 :(得分:0)

使用模板可以简化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>

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

以下是完整的XSLT和输出。

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

输出

approved_request_ids = SecretarioAprovacao.objects.filter(secretario_aprovacao=True).values_list('id', flat=True)
context['solicitacoes'] = Solicitacao.objects.filter(id__in=approved_request_ids)