XSLT: tips and tricks: how to generate node1, node2, …, nodeN

This time let me show you how to generate several similar XML elements like name1, name2, name3, …, nameN.

Input:

<?xml version="1.0" encoding="utf-8"?>
<names>
	<name>John Smith</name>
	<name>Bill Murray</name>
	<name>Anne Williams</name>
	<name>Henry Ford</name>
</names>

Expected output:

<?xml version="1.0" encoding="utf-8"?>
<root>
	<NAME1>John Smith</NAME1>
	<NAME2>Bill Murray</NAME2>
	<NAME3>Anne Williams</NAME3>
	<NAME4>Henry Ford</NAME4>
</root>

Almost always developers are trying to create XSLT like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<root>
			<xsl:if test="names/name[1]">
				<NAME1>
					<xsl:value-of select="names/name[1]"/>
				</NAME1>
			</xsl:if>
			<xsl:if test="names/name[2]">
				<NAME2>
					<xsl:value-of select="names/name[2]"/>
				</NAME2>
			</xsl:if>
			...
			<xsl:if test="names/name[N]">
				<NAMEN>
					<xsl:value-of select="names/name[N]"/>
				</NAMEN>
			</xsl:if>
		</root>
	</xsl:template>
</xsl:stylesheet>

But there is a short and elegant way:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
		<root>
			<xsl:for-each select="names/name">
				<xsl:element name="{concat('NAME', position())}">
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:for-each>
		</root>
    </xsl:template>
</xsl:stylesheet>

Gennady Kim

Advertisements

Building Specs/Designs/MRGs automatically (from XML data)

I’ve enhanced a little the XSLT described here. First version could build the structures only, and we decided to add DDF files (Gentran/GIS/SI model format) as the formats (X12, EDIFACT, IDocs, etc) libraries. Now we can get not only the structure, but also descriptions, data types, optional/mandatory and qualifiers with descriptions. These specs were built automatically, I added colored backgrounds only:

X12:
spec_x12
Continue reading

Barcodes Format

I used to work with different labels, packing slips and other documents which contained barcodes. Often I had to recognize the formats of those barcodes (for example, when client had the scanned image only and had no idea about the format they used).
barcode
In these cases I cut out the barcodes
barcode
and used sites like www.onlinebarcodereader.com to recognize the formats. For example, in this case it’s Code 39 🙂

.

Gennady Kim