[英]Need help - ADD/Replace/Remove nodes in XML based on conditions using XSLT
我必須使用XSLT轉換以下兩個XML(輸入XML#1和輸入XML#2)。 當我在尋找解決方案時,我需要您的幫助來使用XSLT對其進行轉換。
在此先感謝您的幫助。
輸入XML#1
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
<Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>
在XML之上進行轉換的條件是
如果XML#1中存在Last_Name節點,則該節點在轉換后應保留Last_Formatted_Name的值,並從XML#1中刪除Last_Formatted_Name節點。
輸出應如下所示。 請注意Last_Name保留Last_Formatted_Name的值,並且Last_Formatted_Name被刪除。
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
</Summary_Information>
</Employee>
輸入XML#2
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
<Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>
轉換XML#2的條件是:如果輸入XML中不存在Last_Name,則應在First_Name節點之后立即創建一個節點Last_Name,並應保留Last_Formatted_Name的值
輸出應如下所示。 請注意,在刪除First_Name和Last_Formatted_Name節點之后立即創建Last_Name節點。
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
</Summary_Information>
</Employee>
我為達到此目的而編寫了XSLT,但沒有產生我想要的輸出。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()" mode="#default">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Personal">
<xsl:if test="exists(Last_Name)">
<xsl:copy>
<xsl:apply-templates/>
<Last_Name>
<xsl:value-of select="//Last_Formatted_Name"/>
</Last_Name>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="Personal">
<xsl:if test="not(Last_Name)">
<xsl:copy>
<xsl:apply-templates/>
<Last_Name>
<xsl:value-of select="//Last_Formatted_Name"/>
</Last_Name>
</xsl:copy>
</xsl:if>
</xsl:template>
請嘗試以下XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- identity transform -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<!-- modifies the value of <Last_Name> if it exists -->
<xsl:template match="Last_Name">
<xsl:copy>
<xsl:value-of select="../../Summary_Information/Last_Formatted_Name" />
</xsl:copy>
</xsl:template>
<!-- if <Last_Name> does not exist, creates a last name with the appropriate value -->
<xsl:template match="Personal[not(Last_Name)]">
<xsl:copy>
<xsl:apply-templates />
<Last_Name>
<xsl:value-of select="../Summary_Information/Last_Formatted_Name" />
</Last_Name>
</xsl:copy>
</xsl:template>
<!-- removes the node -->
<xsl:template match="Last_Formatted_Name" />
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.