I was trying to reiterate a segment based on a value inside a quantity tag in the value of the xml. This will then again be used to increment the value of another 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 this case I need to reiterate the Stock Movement data 100 times and increment the 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 the segment 100 time til i reach the end of the serial number range.
Thanks again
How about 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 that this assumes that every StockMovementData
has a Quantity
of more than 1 and a Serial
containing a range of numbers.
If not, you could restrict the template to match="StockMovementData[Quantity > 1]"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.