<xsl:template match="hcl:ShipmentOrder"> <xsl:for-each select="hcl:Packages/hcl:Package"> <xsl:value-of select="../../hcl:OrderID"/>, <xsl:value-of select="hcl:TrackingNumber"/>, <xsl:for-each select="hcl:Items/hcl:Item"> <xsl:value-of select="hcl:SKU"/>, <xsl:value-of select="hcl:Qty"/> <xsl:if test="not(position()=last())">|</xsl:if> </xsl:for-each> <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> He was mixing a little imperative (the for-each ) with the declarative, and he didn't care. It was his solution.
Saturday morning, 8:00 AM. Coffee in hand, Leo opened Udemy and stared into the abyss. "The Complete XSLT Course: From Zero to Hero" by a British instructor named Alistair Finch. 4.6 stars. 14,000 students. 18.5 hours of video. Leo's eye twitched. He’d been burned before by "complete" courses that spent three hours on "What is a variable?"
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> It looked like magic. A recursive mirror. Leo stared at it for ten minutes, tracing the logic. Then he had his Eureka moment. This is the power of XSLT. You don't iterate with for-each (Alistair called that "imperative blasphemy"). You let the templates find the nodes and decide their fate.
Leo laughed, cracked open a beer, and added "XSLT" to his LinkedIn profile. He was no longer a data plumber. He was a lumberjack. And it was a good day.
Sunday morning. The final boss. He needed to generate a CSV header row, then loop through each ShipmentOrder , and for each Package , produce a line with OrderID, TrackingNumber, ItemSKU, Quantity . But some Package elements had no Item (empty shipments), and some had ten.
He wrote his final template:
He uploaded the XSLT to the production mapper, ran a test with a real 500MB XML file, and watched it transform in 2.3 seconds. His boss, Sarah, pinged him on Slack. Sarah: "Did you get the XSLT working?" Leo: "Yeah. It's done." Sarah: "You learned XSLT in a weekend?" Leo: "I had a good instructor." He closed his laptop, looked at the sticky note on his monitor – You are always somewhere. Know where. – and smiled. He opened Udemy one last time. A notification popped up.
Sunday, 9:00 PM. Leo ran his transformation. Saxon-HE (the XSLT processor Alistair had recommended) hummed. The output file appeared: output.csv . He opened it.
Drainage Wakefield