我有一個XMl,我想根據條件刪除它的某些部分。 的XML如下:XSLT條件模板轉換
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<MessageID>0a9d3ba5-9025-4e24-a0de-2403a0c0919b</MessageID>
<MessageDate>2015-04-10</MessageDate>
<PPMVersion>6.0.0.0</PPMVersion>
<SchemaVersion>1.0</SchemaVersion>
</Header>
<Package xsi:type="Ritter_Sales_Template" Path="/Package/Product/Launch_Entity" BusinessID="010170" ID="a13b6861-bb32-47fa-8ef4-f0ddcc0bc134">
<Name>MOTO_pack</Name>
<Category_ID Product_Line_ID="5">5</Category_ID>
<Effective_Start_Date>2015-02-23</Effective_Start_Date>
<Available_Start_Date>2015-02-23</Available_Start_Date>
<Element_Guid>a13b6861-bb32-47fa-8ef4-f0ddcc0bc134</Element_Guid>
<Element_Type_Guid>f7e69de1-23e8-4a66-b57b-d849e989e1ce</Element_Type_Guid>
<Business_ID>010170</Business_ID>
<Product_Name>MOTO_pack</Product_Name>
<Product_To_Product xsi:type="Product_Relation" ID="38224fc0-1a65-4b8e-9985-2277c99bbebf" Pattern="Relation_Entity">
<Association_Start_Date>2015-02-23</Association_Start_Date>
<Association_End_Date>2015-04-15</Association_End_Date>
<Product xsi:type="New_Test_template" Path="/Component/Product/Launch_Entity" BusinessID="009154" ID="da80790a-0523-472c-89b0-208d919fcf73">
<Name>idea_test</Name>
<Category_ID Product_Line_ID="5">8</Category_ID>
<Effective_Start_Date>2015-02-19</Effective_Start_Date>
<Available_Start_Date>2015-02-19</Available_Start_Date>
<Element_Guid>da80790a-0523-472c-89b0-208d919fcf73</Element_Guid>
<Element_Type_Guid>0a7fb9ea-95c4-4eed-a716-ca588f8dcae8</Element_Type_Guid>
<Business_ID>009154</Business_ID>
<Product_Name>idea_test</Product_Name>
<charge_role xsi:type="Lookup_Charge_Role" ID="6920b661-63de-45a0-94d4-900a4a873632" Pattern="Lookup">
<Name>Recipient</Name>
<Description>Recipient</Description>
</charge_role>
</Product>
</Product_To_Product>
<Product_To_Product xsi:type="Product_Relation" ID="eae5a5fd-1a27-4832-bdbe-c77907a77538" Pattern="Relation_Entity">
<Association_Start_Date>2015-02-23</Association_Start_Date>
<Association_End_Date>2015-04-10</Association_End_Date>
<Product xsi:type="Component_Template" Path="/Product_Component/Component/Product/Launch_Entity" BusinessID="010169" ID="9f4a5a5b-1cb4-463b-b211-98f08fec2ce7">
<Name>Moto-g</Name>
<Category_ID Product_Line_ID="5">8</Category_ID>
<Effective_Start_Date>2015-02-22</Effective_Start_Date>
<Available_Start_Date>2015-02-23</Available_Start_Date>
<Element_Guid>9f4a5a5b-1cb4-463b-b211-98f08fec2ce7</Element_Guid>
<Element_Type_Guid>26c0d089-259b-444b-842a-e42d21dea778</Element_Type_Guid>
<Business_ID>010169</Business_ID>
<Product_Name>Moto-g</Product_Name>
<Product_To_Product xsi:type="Product_Relation" ID="96ef93fd-ba40-4628-b22a-92c03e606a89" Pattern="Relation_Entity">
<Association_Start_Date>2015-02-23</Association_Start_Date>
<Product xsi:type="New_Test_template" Path="/Component/Product/Launch_Entity" BusinessID="009154" ID="da80790a-0523-472c-89b0-208d919fcf73">
<Name>idea_test</Name>
<Category_ID Product_Line_ID="5">8</Category_ID>
<Effective_Start_Date>2015-02-19</Effective_Start_Date>
<Available_Start_Date>2015-02-19</Available_Start_Date>
<Element_Guid>da80790a-0523-472c-89b0-208d919fcf73</Element_Guid>
<Element_Type_Guid>0a7fb9ea-95c4-4eed-a716-ca588f8dcae8</Element_Type_Guid>
<Business_ID>009154</Business_ID>
<Product_Name>idea_test</Product_Name>
<charge_role xsi:type="Lookup_Charge_Role" ID="6920b661-63de-45a0-94d4-900a4a873632" Pattern="Lookup">
<Name>Recipient</Name>
<Description>Recipient</Description>
</charge_role>
</Product>
</Product_To_Product>
</Product>
</Product_To_Product>
</Package>
</Root>
在這裏,我想刪除整個Product_To_Product元素,如果有任何產品Association_End_Date是過去陳舊。因爲我寫了下面的代碼,我通過C#函數比較日期,但我仍然無法刪除實體。請提出我出錯的地方。 XSLT代碼:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl
="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:cs
="urn:cs">
<xsl:output method="xml" version="1.0" encoding="UTF‐8" indent="yes"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[public static long compareDate(long Asso_End_Date)
{
DateTime date1 = new DateTime(Asso_End_Date);
DateTime date2 = DateTime.Now;
long result;
result = date2.Date.CompareTo(date1.Date);
return(result);
}]]>
</msxsl:script>
<!-- TEMPLATE #1 -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- TEMPLATE #2 -->
<xsl:template name="mode1">
<xsl:apply-templates select="Product_To_Product[Association_End_Date]"/>
</xsl:template>
<xsl:template name="mode2">
<xsl:variable name="date"/><xsl:value-of select="Product_To_Product[Association_End_Date]"/>
<xsl:choose>
<xsl:when test="cs:compareDate($date) > 0">
<xsl:call-template name='mode1'/>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
嘗試將''更改爲''並且您不需要模板' .. xsl:template>' –
我試着改變它,但它仍然不能工作,如果我改變它然後它會首先做匹配和條件仍未被檢查。我想如果條件滿足,那麼元素應該被刪除。 –