[xsl] Up-conversion using XSLT 1.0

Subject: [xsl] Up-conversion using XSLT 1.0
From: Manfred Staudinger <manfred.staudinger@xxxxxxxxx>
Date: Mon, 17 Oct 2005 09:16:10 -0700
Hi list,
its not exactly an up-conversion, because the input docs should stay
with a minimum of mark-up and I'm currently in the process to define
that minimal set. The aim of the transformations then is to provide the
docs temporarily in a form suitable to a program, in this case a browser.
 -  Anyway, I've the following input:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="../view-list.xsl" type="text/xsl"?>
<doc>
text-1
text-1
<div class="index">
elements in div
</div>
   text-2
   text-2
<ul>
text in ul
text in ul
</ul>
   text-3-1
   text-3-2
   text-3-3
text-4-1
text-4-2
</doc>
There can be 0-10 ul-elements anywhere between
text-2 and text-3. The lines "text in ul" are processed
by using a named template (not shown here).

With the stylesheet
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes" />
<xsl:variable name="LF" select="'&#xA;'" />
<xsl:template match="doc">
	<xsl:apply-templates select="node()|@*" />
</xsl:template>
<xsl:template match="text()">
	<xsl:call-template name="insert-list">
		<xsl:with-param name="list" select="." />
	</xsl:call-template>
</xsl:template>
<xsl:template match="doc/div[@class='index']">
	<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="doc/ul">
	<ul><xsl:value-of select="."/></ul>
</xsl:template>

<xsl:template match="/">
<body>
	<xsl:value-of select="$LF" />
	<xsl:apply-templates select="node()|@*" />
</body>
</xsl:template>

<xsl:template name="insert-list">
<xsl:param name="list"/>
<xsl:param name="list2" select="''"/>
	<xsl:variable name="line" select="substring-before($list, $LF)"/>
	<xsl:choose>
		<xsl:when test="starts-with($line,'   ')">
			<xsl:call-template name="insert-list">
				<xsl:with-param name="list" select="substring-after($list, $LF)" />
				<xsl:with-param name="list2" select="concat($list2,
substring($line, 4), $LF)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="string-length($list2)>0">
			<zitat>
			<xsl:value-of select="$LF" />
			<xsl:call-template name="insert-list">
				<xsl:with-param name="list" select="$list2"/>
			</xsl:call-template>
			</zitat>
			<xsl:value-of select="$LF" />
			<xsl:call-template name="insert-list">
				<xsl:with-param name="list" select="$list"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="string-length($list)=0">
		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="string-length($line)>0">
				<xsl:value-of select="concat('+',$line,$LF)" />
			</xsl:if>
			<xsl:call-template name="insert-list">
				<xsl:with-param name="list" select="substring-after($list, $LF)" />
				<xsl:with-param name="list2" select="$list2"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
</xsl:stylesheet>

I get the following output:
<?xml version="1.0" encoding="utf-8"?>
<body>
+text-1
+text-1
<div class="index">
elements in div
</div>
   <zitat>
+text-2
+text-2
</zitat>

   <ul>
text in ul
text in ul
</ul>
   <zitat>
+text-3-1
+text-3-2
+text-3-3
</zitat>
+text-4-1
+text-4-2
</body>


This is what I would like to get, but I've no idea how to do:
<?xml version="1.0" encoding="utf-8"?>
<body>
+text-1
+text-1
<div class="index">
elements in div
</div>
   <zitat>
+text-2
+text-2
   <ul>
text in ul
text in ul
</ul>
+text-3-1
+text-3-2
+text-3-3
</zitat>
+text-4-1
+text-4-2
</body>

Any comments will be appreciated.
Regards, Manfred Staudinger, Vienna

Current Thread