c# - XSLT Conditional Template transformation -
i having 1 xml, want remove portion of based upon condition. xml follows :
<?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>
in here, want remove whole product_to_product element if association_end_date product past dated. have written following code in comparing dates through c# function, still unable remove entity. please suggest going wrong. xslt code :
<?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>
there number of issues xslt. first, mentioned lingamurthy cs in comments, have named template, called mode2
, nothing calling template, never gets used. should changed template match
<xsl:template match="product_to_product[association_end_date]">
secondly, have problem how define date
variable.
<xsl:variable name="date"/>
in other words, don't define it. empty. xsl:value-of
follows variable declaration won't affect @ all. should define so
<xsl:variable name="date" select="association_end_date"/>
thirdly, there problem c# function itself
public static long comparedate(long asso_end_date)
the parameter has type long
, not passing in long
, string. function should this, can parse string
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) }
also, need replace >
<
when checking results of call.
try xslt (also note how mode1
template not needed.
<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) <= 0"> <xsl:call-template name="identity" /> </xsl:when> <xsl:otherwise> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
or better still, put condition in template match, simplify so:
<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) > 0]" /> </xsl:stylesheet>
Comments
Post a Comment