Internet Sellout

Demand Unearned Rewards

XSLT 1.0 Solutions

JavaScript and JSON are the fashion now but once upon a time there was XML. XSLT is part of the XML ecosystem. I've never been absolutely sure where one of these: XML, XSLT, XPATH or XSL begins or ends. How do I interpret

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"></xsl:stylesheet>

 XSL Templates, XSL Transforms... not surprised this wasn't the one ring to rule them all. But there were some uses that were more appropriate than others and have lived on. You can see on the internet searching in 2017 for XSLT samples that most of the recent activity was 2010. Its gonna be hard to find good samples one day. If your XSLT implementation is still around there is a chance its version 1.0. Lets start a post devoted to the greatest hits:

Copy Distinct Nodes

<xsl:element name="table">
	<xsl:for-each select="dta/table/row[not(.=preceding-sibling::row)]">
		<xsl:element name="row">
			<xsl:copy-of select="./*"></xsl:copy-of>
		</xsl:element>
	</xsl:for-each>
</xsl:element>

The target data is a Microsoft XSD representation of data where you have a table with rows that have child nodes representing columns. Lets say one of those child nodes is the only difference between some of those rows and you would like to eliminate the redundancy. First copy the row data except for the child node you are going to eliminate (blank it out or something) leaving you with several rows with identical data. This code above will copy unique rows (nodes).

Get Data From Other Dataset Using Keys

<xsl:element name="table">
	<xsl:for-each select="data2/table/row">
		<xsl:variable name="Angry" select="Angry"></xsl:variable>
		<xsl:variable name="Bloody" select="Bloody"></xsl:variable>
		<xsl:variable name="Chicken" select="Chicken"></xsl:variable>
		<xsl:element name="row">
			<xsl:element name="DoTheNeedful">
				<xsl:value-of select="data1/table/row/Angry[text() = $Angry]/../Bloody[text() = $Bloody]/../Chicken[text() = $Chicken]/../DoTheNeedful"></xsl:value-of>
			</xsl:element>
			<xsl:element name="Angry">
				<xsl:value-of select="Angry"></xsl:value-of>
			</xsl:element>
			<xsl:element name="Bloody">
				<xsl:value-of select="Bloody"></xsl:value-of>
			</xsl:element>
			<xsl:element name="Chicken">
				<xsl:value-of select="Chicken"></xsl:value-of>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
</xsl:element>

In this example above you have two datasets with tables and rows of a similar schema with Angry, Bloody and Chicken as Keys and DoTheNeedful as a value. Data1 has the DoTheNeedful you want so you match the keys and get the value for Data2.

Next time: Someone wants me to be afraid and angry at the Russians.

 

Comments are closed