How to reiterate an XML based on a value in the segment tag in xslt? mule? -
i trying reiterate segment based on value inside quantity tag in value of xml. again used increment value of tag.
input
<?xml version="1.0" encoding="utf-8"?> <stockmovementdatarequest xmlns:a="http://www.edi.com.au/enterpriseservice/" xmlns:p1="urn:ams.com.au:dynamo:3pl:am:sap_am_i_005:stockmovement"> <header> <from>awh</from> <to>sap</to> <unique_id>9abb2454-068d-11e6-8f1b-509e20524153</unique_id> <datetimestamp>2016-04-19t12:30:16.52+10:00</datetimestamp> </header> <stockmovementdata> <serialised_material>yes</serialised_material> <datetime>2016-04-19t12:30:16.52+10:00</datetime> <from_location>0030-0080</from_location> <to_location>actstrbne</to_location> <material>7cagl3g01</material> <serial>700032961 - 700033060 #4</serial> <quantity>100</quantity> </stockmovementdata> </stockmovementdatarequest>
for case need reiterate stock movement data 100 times , increment serial tag
target
<?xml version="1.0" encoding="utf-8"?> <stockmovementdatarequest xmlns:a="http://www.edi.com.au/enterpriseservice/" xmlns:p1="urn:ams.com.au:dynamo:3pl:am:sap_am_i_005:stockmovement"> <header> <from>awh</from> <to>sap</to> <unique_id>9abb2454-068d-11e6-8f1b-509e20524153</unique_id> <datetimestamp>2016-04-19t12:30:16.52+10:00</datetimestamp> </header> <stockmovementdata> <serialised_material>yes</serialised_material> <datetime>2016-04-19t12:30:16.52+10:00</datetime> <from_location>0030-0080</from_location> <to_location>actstrbne</to_location> <material>7cagl3g01</material> <serial>700032561</serial> <quantity>1</quantity> </stockmovementdata> <stockmovementdata> <serialised_material>yes</serialised_material> <datetime>2016-04-19t12:30:16.52+10:00</datetime> <from_location>0030-0080</from_location> <to_location>actstrbne</to_location> <material>7cagl3g01</material> <serial>700032562</serial> <quantity>1</quantity> </stockmovementdata> </stockmovementdatarequest>
xsl translation
<?xml version='1.0' ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"><xsl:variable name="serial" select="substring(stockmovementdatarequest/stockmovementdata/serial, 0, 9)" /><xsl:variable name="count" select="stockmovementdatarequest/stockmovementdata/quantity" /><xsl:variable name="temp" select="1" /> <xsl:output method="xml" indent="yes"/> <xsl:call-template name="selects"> <xsl:with-param name="i">1</xsl:with-param> <xsl:with-param name="counter"> <xsl:value-of select="$count" /> </xsl:with-param> </xsl:call-template> <xsl:template match="/"> <stockmovementdatarequest xmlns:p1="urn:ams.com.au:dynamo:3pl:am:sap_am_i_005:stockmovement" xmlns:a="http://www.edi.com.au/enterpriseservice/"> <header> <from> <xsl:value-of select="stockmovementdatarequest/header/from"/> </from> <to> <xsl:value-of select="stockmovementdatarequest/header/to"/> </to> <unique_id> <xsl:value-of select="stockmovementdatarequest/header/unique_id"/> </unique_id> <datetimestamp> <xsl:value-of select="stockmovementdatarequest/header/datetimestamp"/> </datetimestamp> </header> <xsl:template name="stockmovementdata"> <xsl:param name="i" /> <xsl:param name="counter" /> <xsl:if test="$i <= $counter"> <stockmovementdata> <xsl:apply-templates select="stockmovementdatarequest/stockmovementdata"/> </stockmovementdata> </xsl:if> <!--begin_: repeattheloopuntilfinished--> <xsl:if test="$i <= $counter"> <xsl:call-template name="stockmovementdata"> <xsl:with-param name="i"> <xsl:value-of select="$i + 1"/> </xsl:with-param> <xsl:with-param name="counter"> <xsl:value-of select="$counter"/> </xsl:with-param> </xsl:call-template> </xsl:if> </xsl:template> </stockmovementdatarequest> </xsl:template> <xsl:template match="stockmovementdatarequest/stockmovementdata"> <serialised_material> <xsl:value-of select="serialised_material"/> </serialised_material> <datetime> <xsl:value-of select="datetime"/> </datetime> <from_location> <xsl:value-of select="from_location"/> </from_location> <to_location> <xsl:value-of select="to_location"/> </to_location> <material> <xsl:value-of select="material"/> </material> <serial> <xsl:value-of select="$serial"/> </serial> <quantity> <xsl:value-of select="1" /> </quantity> </xsl:template> </xsl:stylesheet>
and repeat segment 100 time til reach end of serial number range.
thanks again
how simply:
xslt 1.0
<xsl:stylesheet version="1.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="stockmovementdata" name="replicate"> <xsl:param name="i" select="1"/> <xsl:copy> <xsl:apply-templates select="@*|node()[not(self::serial or self::quantity)]"/> <serial> <xsl:value-of select="substring-before(serial, ' -' ) + $i - 1"/> </serial> <quantity>1</quantity> </xsl:copy> <xsl:if test="$i < quantity"> <xsl:call-template name="replicate"> <xsl:with-param name="i" select="$i + 1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
note assumes every stockmovementdata
has quantity
of more 1 , serial
containing range of numbers.
if not, restrict template match="stockmovementdata[quantity > 1]"
.
Comments
Post a Comment