![](/img/trans.png)
[英]How to remove common nodes from XML files based on same attributes of input XML request using XSLT?
[英]XSLT 2.0: Remove nodes from input XML based on condition
我有一個 XML,它可能包含多個 employment_information 節點。 我需要 select 子字段 assignment_class 為“GA”的那個。 應丟棄其他 employment_information 節點。 此外,如果只有一個 employment_information 節點,則應按原樣傳遞。
輸入 XML:
<?xml version="1.0" encoding="utf-8"?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<Person>
<StartDate>2012-02-01</StartDate>
<EndDate>2019-02-28</EndDate>
<action>NO CHANGE</action>
<employment_information>
<action>NO CHANGE</action>
<assignment_class>ST</assignment_class>
</employment_information>
<employment_information>
<action>NO CHANGE</action>
<assignment_class>GA</assignment_class>
</employment_information>
</Person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>
預計 Output:
<?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<Person>
<StartDate>2012-02-01</StartDate>
<EndDate>2019-02-28</EndDate>
<action>NO CHANGE</action>
<employment_information>
<action>NO CHANGE</action>
<assignment_class>GA</assignment_class>
</employment_information>
</Person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>
在參考了一些在線帖子后,我部分能夠使用以下 XSLT 實現此目的:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//Person/employment_information[assignment_class!='GA']"/>
</xsl:stylesheet>
但我需要增強這個 XSLT 最終具有以下邏輯:
任何幫助表示贊賞。 謝謝!
如果我沒看錯的話,您可以通過以下一種方式查看它:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="employment_information[assignment_class!='GA' and count(../employment_information) gt 1]"/>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.