使用XSL删除XML元素

时间:2013-03-05 22:06:30

标签: xml xslt

我有一个XSL脚本,它读取XML文件以获取元素中的值,创建另一个要在下游使用的XML。

原始XML文件的输出示例如下:

<e:OnboardingProcess>
    <e:Assignment>
        <e:Assignment>
            <e:Application>
                <e:Application>
                    <e:CandidateIndentificationNo>7047961</e:CandidateIndentificationNo>
                </e:Application>
            </e:Application>
            <e:Offer>
                <e:Offer>
                    <e:UDFs>
                        <e:UDF name="FRB_5fOnboarding_5fLocation">
                            <e:CustomSelectionElement>
                                <e:Code>1204</e:Code>
                            </e:CustomSelectionElement>
                        </e:UDF>
                    </e:UDFs>
                </e:Offer>
            </e:Offer>
            <e:TalentMasterFile>
                <e:TalentMasterFile>
                    <e:User>
                        <e:User>
                            <e:EmployeeID></e:EmployeeID>
                            <e:FirstName>Peter</e:FirstName>
                            <e:LastName>Griffin</e:LastName>
                        </e:User>
                    </e:User>
                </e:TalentMasterFile>
            </e:TalentMasterFile>

我的XSL脚本有代码来引用一些值,它看起来像这样:

<xsl:variable name="CandidateNumber" select="../../e:Assignment/e:Assignment/e:Application/e:Application/e:CandidateIndentificationNo"/>
<xsl:variable name="FirstName" select="../../e:Assignment/e:Assignment/e:TalentMasterFile/e:TalentMasterFile/e:User/e:User/e:FirstName"/>
<xsl:variable name="LastName" select="../../e:Assignment/e:Assignment/e:TalentMasterFile/e:TalentMasterFile/e:User/e:User/e:LastName"/>
<xsl:variable name="Location" select="../../e:Assignment/e:Assignment/e:Offer/e:Offer/e:UDFs/e:CustomSelectionElement/e:Code"/>

它适用于除“位置代码”之外的所有内容,唯一的区别是该位置在<e:UDF name="FRB_5fOnboarding_5fLocation">之前有一个特殊元素。有没有人知道如何映射到位置代码或如何编写程序来获取原始XML文件,删除所有出现的<e:UDF name="FRB_5fOnboarding_5fLocation">元素,并输出一个看起来与第一个相同的新XML文件除了没有不需要的元素。

1 个答案:

答案 0 :(得分:1)

您对位置元素的XPath缺少e:UDF元素步骤。

<xsl:variable name="Location" select="../../e:Assignment/e:Assignment/e:Offer/e:Offer/e:UDFs/e:CustomSelectionElement/e:Code"/>

实际应该是

<xsl:variable name="Location" select="../../e:Assignment/e:Assignment/e:Offer/e:Offer/e:UDFs/e:UDF/e:CustomSelectionElement/e:Code"/>