Auxiliary XSLT for building specs/MRGs for XML

Here is a simple XSLT which helps me to “extract” the structure from XML file. I use it for building specifications/designs/MRGs in MS Excel format:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:variable name="default_indent" select="'& #x9;'" />
	<xsl:template match="/">
		<xsl:for-each select=".">
			<xsl:call-template name="processElement" />
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="processElement">
		<xsl:param name="indent" select="''" />

		<xsl:value-of select="$indent"/>
		<xsl:value-of select="name()"/>
		<xsl:text>& #xA;</xsl:text>
		
		<xsl:for-each select="@*">
			<xsl:value-of select="concat($indent, $default_indent)"/>
			<xsl:value-of select="'@'"/>
			<xsl:value-of select="name()"/>
			<xsl:text>& #xA;</xsl:text>
		</xsl:for-each>

		<xsl:if test="count(./*) != 0">
			<xsl:for-each select="./*">
				<xsl:call-template name="processElement">
					<xsl:with-param name="indent">
						<xsl:value-of select="concat($indent, $default_indent)"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>


(an extra space between the ampersands and entity values should be removed)

Input:

<?xml version="1.0"?>
<ST>
	<ST01>
		<code>850</code>
	</ST01>
	<ST02>0100</ST02>
	<BEG>
		<BEG01>
			<code>00</code>
		</BEG01>
		<BEG02>
			<code>SA</code>
		</BEG02>
		<BEG03>1234567890</BEG03>
		<BEG05>20120516</BEG05>
	</BEG>
	<REF>
		<REF01>
			<code>IA</code>
		</REF01>
		<REF02>GREE</REF02>
	</REF>
	<N9>
		<N901>
			<code>L1</code>
		</N901>
		<N902>ORDER COMMENTS</N902>
		<MSG>
			<MSG01>STORE ORDERING FORM</MSG01>
		</MSG>
...

Output:

	ST
		ST01
			code
		ST02
		BEG
			BEG01
				code
			BEG02
				code
			BEG03
			BEG05
		REF
			REF01
				code
			REF02
		N9
			N901
				code
			N902
			MSG
				MSG01
			MSG
				MSG01
		N1
			N101
				code
			N102
			N103
				code
			N104
...

MS Excel:

Gennady Kim

Advertisements

One thought on “Auxiliary XSLT for building specs/MRGs for XML

  1. Pingback: Building Specs/Designs/MRGs automatically (XML) | EDI shortcuts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s