Re: [xsl] Convert Word XML to Hierarchical XML using XSLT

Subject: Re: [xsl] Convert Word XML to Hierarchical XML using XSLT
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Fri, 16 Sep 2005 12:21:09 +0200
Hi,

Tempore 11:40:05, die 09/16/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Rod Coate <Rod.Coate@xxxxxxxxxxxxxxxxx>:

I need a bit of Guidance as to how to approach this problem. If I was coding in VBA then I could just count and compare Heading levels, but how do I do this in XSL?

The ouput XML does not seem to have a recursive structure.


You'll have to specify the instructions in a non-generic way:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes"/>

<xsl:template match="Word-Document">
	<root>
		<xsl:apply-templates select="Heading1"/>
	</root>
</xsl:template>

<xsl:template match="Heading1">
	<xsl:copy>
		<Title><xsl:apply-templates/></Title>
		<Data>
			<xsl:apply-templates select="following::*[1]" mode="recursive">
				<xsl:with-param name="parents" select="local-name()"/>
				<xsl:with-param name="level" select="'Heading2'"/>
			</xsl:apply-templates>
		</Data>
	</xsl:copy>
</xsl:template>

<xsl:template match="Heading2">
<xsl:param name="parents"/>
	<xsl:copy><xsl:apply-templates/></xsl:copy>
	<xsl:apply-templates select="following::*[1]" mode="recursive">
		<xsl:with-param name="parents" select="concat($parents,'-',local-name())"/>
		<xsl:with-param name="level" select="'Heading3'"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="Heading3">
<xsl:param name="parents"/>
	<xsl:element name="{.}">
		<xsl:apply-templates select="following::*[1]" mode="recursive">
			<xsl:with-param name="parents" select="concat($parents,'-',local-name())"/>
			<xsl:with-param name="level" select="'Normal'"/>
		</xsl:apply-templates>	
	</xsl:element>
</xsl:template>

<xsl:template match="*" mode="recursive">
	<xsl:param name="level"/>
	<xsl:param name="parents"/>
	<xsl:if test="local-name()=$level">
		<xsl:apply-templates select=".">
			<xsl:with-param name="parents" select="$parents"/>
		</xsl:apply-templates>
	</xsl:if>
	<xsl:if test="not(contains($parents,local-name()))">
		<xsl:apply-templates select="following::*[1]" mode="recursive">
			<xsl:with-param name="level" select="$level"/>
			<xsl:with-param name="parents" select="$parents"/>
		</xsl:apply-templates>	
	</xsl:if>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Gaudiam omnibus traderat W3C, nec vana fides

Current Thread