如何删除外部XML文件中引用的特定节点和子节点?

时间:2015-09-17 09:15:41

标签: xml xslt

我有一个大型XML文件,我需要删除其中一个实例节点的OWNINST与另一个XML文件中的一个匹配的整个学生节点(以及该学生中的所有子节点)。

我的大型XML文件'file1.xml'采用以下格式:

<institution>
<ukprn>1234</ukprn>
<course>
    <courseID>1</courseID>
    <courseaim>X99</courseaim>
</course>
<student>
    <birthdate>30/10/1985</birthdate>
    <instance>
        <OWNINST>123456|5</OWNINST>
        <FC>1</FC>
        <elq>4</elq>
    </instance>
</student>
<student>
    <birthdate>01/02/1999</birthdate>
    <ybj>76</jbj>
    <instance>
        <OWNINST>654321|1</OWNINST>
        <FC>2</FC>
        <elq>2</elq>
    </instance>
    <instance>
        <OWNINST>654321|2</OWNINST>
        <FC>6</FC>
        <elq>1</elq>
    </instance>
</student>

有多个学生,每个学生可以有多个实例,并非所有学生都有所有节点。

我有另一个xml文件'File2.xml',其结构如下:

<studentstodelete>
    <OWNINST>555466|2</OWNINST>
    <OWNINST>654321|1</OWNINST>
</studentstodelete>

对于File2.xml中的每个学生,如果他们的OWNINST列在File2.xml中,我想删除它们在File1.xml中的整个学生节点(包括子节点)。不应更改未列在File2.xml中的任何学生。

请有人帮忙解决这个问题,因为我不知道如何告诉它删除记录。

这是我为样式表提出的内容,但缺少“删除”位:

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

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

<xsl:template match="Student/Instance[OWNINST = document('File2.xml')/studentstodelete/OWNINST]">
</xsl:template>

任何帮助都非常感激。

由于

1 个答案:

答案 0 :(得分:0)

如果要删除Student元素,则需要更改

<xsl:template match="Student/Instance[OWNINST = document('File2.xml')/studentstodelete/OWNINST]">
</xsl:template>

<xsl:template match="Student[Instance/OWNINST = document('File2.xml')/studentstodelete/OWNINST]"></xsl:template>
相关问题