2015-04-15 118 views
0

我有一個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) &gt; 0"> 
     <xsl:call-template name='mode1'/> 
    </xsl:when> 
    <xsl:otherwise> 
    </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 
+0

嘗試將''更改爲''並且您不需要模板' ..' –

+0

我試着改變它,但它仍然不能工作,如果我改變它然後它會首先做匹配和條件仍未被檢查。我想如果條件滿足,那麼元素應該被刪除。 –

回答

0

您的XSLT有許多問題。首先,正如Lingamurthy CS在評論中提到的那樣,您有一個名爲mode2的已命名模板,但沒有任何內容正在調用該模板,因此它永遠不會被使用。它應該被改爲模板匹配

<xsl:template match="Product_To_Product[Association_End_Date]"> 

其次,你有你如何定義你的date變量的一個問題。

<xsl:variable name="date"/> 

換句話說,你沒有定義它。它實際上是空的。遵循變量聲明的xsl:value-of根本不會影響它。你應該定義它像這樣

<xsl:variable name="date" select="Association_End_Date"/> 

第三,有一個與你的C#功能的問題本身

public static long compareDate(long Asso_End_Date) 

參數具有類型long,但你不能傳遞long,而是一個字符串。該功能確實應該是這樣的,這樣的話它可以解析字符串

public static long compareDate(string Asso_End_Date) 
{ 
DateTime date1 = DateTime.Parse(Asso_End_Date); 
DateTime date2 = DateTime.Now; 
long result; 
result = date2.Date.CompareTo(date1.Date); 
return(result) 
} 

此外,您檢查調用的結果時,可能需要使用&lt;更換&gt;

試試這個XSLT(也注意不是如何需要mode1模板

<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(string Asso_End_Date) 
{ 
DateTime date1 = DateTime.Parse(Asso_End_Date); 
DateTime date2 = DateTime.Now; 
long result; 
result = date2.Date.CompareTo(date1.Date); 
return(result); 
}]]> 
</msxsl:script> 

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

    <xsl:template match="Product_To_Product[Association_End_Date]"> 
    <xsl:variable name="date" select="Association_End_Date"/> 
    <xsl:choose> 
     <xsl:when test="cs:compareDate($date) &lt;= 0"> 
      <xsl:call-template name="identity" /> 
     </xsl:when> 
     <xsl:otherwise> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

或者更好的是,把條件在模板匹配,簡化它,像這樣:

<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(string Asso_End_Date) 
{ 
DateTime date1 = DateTime.Parse(Asso_End_Date); 
DateTime date2 = DateTime.Now; 
long result; 
result = date2.Date.CompareTo(date1.Date); 
return(result); 
}]]> 
</msxsl:script> 

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

    <xsl:template match="Product_To_Product[cs:compareDate(Association_End_Date) &gt; 0]" /> 
</xsl:stylesheet> 
+0

非常感謝! :)這個作品! –