Re: [xsl] nested XML using xslt

Subject: Re: [xsl] nested XML using xslt
From: kurps s <kurpss@xxxxxxxxx>
Date: Fri, 24 Jul 2009 11:35:26 -0500
Hi

Thank you for all your response. I apologize for not providing the
input, I am new to the group and just getting to know it.

Here is the input file:


<list1>
  <litem>000</litem>
<list2>
<litem>123 </litem>

<list3>
<litem>ABC</litem>
<litem>666</litem>
</list3>
<!-- now this litem element belongs to list 2-->
<litem> 999 I belong to list 2</litem>
<litem> I belong to list 2</litem>
<list3>
<litem> I belong to the new list 3</litem>
</list3>
<litem> I belong to list 2 again</litem>
<list3>
<litem> another list 3</litem>
<list4>
<litem>222</litem>

</list4>
<!-- I belong to lsit 3 now-->
<litem>333</litem>
</list3>
</list2>
</list1>

Here is my desired output:

<level1>
  <text>000</text>
  <level2>
    <text>
      <para>123</para>
    </text>
    <level3>
      <text>
        <para>ABC</para>
      </text>
    </level3>
    <level3>
      <text>
        <para>666</para>
      </text>
    </level3>
  </level2>
  <level2>
    <text>
      <para>999 I belong to list 2</para>
    </text>
  </level2>
  <level2>
    <text>
      <para>I belong to list 2.</para>
    </text>
    <level3>
      <text>
        <para>I belong to the new list 3list 3.</para>
      </text>
    </level3>
  </level2>
  <level2>
    <text>
      <para>I belong to list 2 again</para>
    </text>
    <level3>
      <text>
        <para>another list 3</para>
      </text>
      <level4 id="S022496B8">
        <text>
          <para>222</para>
        </text>
      </level4>
    </level3>
  </level2>
</level1>

The idea is that every 'litem' element counts as a 'levelX'.

Here is what I wrote:
<xsl:template match="litem">
		<level1>
			<text>
				<para>
					<xsl:value-of select="."/>
				</para>
			</text>
		
	
			<xsl:apply-templates select="list2/litem"/>
		</level1>
	</xsl:template>
	<xsl:template match="list2/litem">
		<level2>
			<text>
				<para>
					<xsl:value-of select="."/>
				</para>
			</text>
		</level2>
<xsl:apply-templates select="list3/litem"/>
	</xsl:template>
	<xsl:template match="list3/litem">
		<level3>
			<text>
				<para>
					<xsl:value-of select="."/>
				</para>
			</text>
		</level3>
<xsl:apply-templates select="list4/litem"/>
	</xsl:template>
	<xsl:template match="list4/litem">
		<level4>
			<text>
				<para>
					<xsl:value-of select="."/>
				</para>
			</text>
		</level4>
	</xsl:template>
This is running into a continuous list level intead of opening and
closing list levels approriately.
What  am I doing wrong here?

On Fri, Jul 24, 2009 at 9:43 AM, Wendell Piez<wapiez@xxxxxxxxxxxxxxxx> wrote:
> Hi,
>
> Also, as an earlier respondent noticed, it's impossible to answer this
> question without specifying the source data on which the stylesheet must
> operate.
>
> The confusing thing is that the OP spoke of having to generate nested
> structures, but the templates in his code matched structures with the same
> names as his presumed output.
>
> So which is it: generating nested structures from some (unstated) input, or
> processing nested structures -- or mapping one nested structure to another?
> Any of these may be trivially easy in XSLT, but they aren't the same.
>
> Following the list guidelines and providing illustrations of both source and
> intended result would be a big help here.
>
> Cheers,
> Wendell
>
> At 07:05 PM 7/23/2009, Mike wrote:
>>
>> > The trick is to make sure that the first list3 gets closed
>> > after <list4> , and a new <list3> opens after the first closes.
>>
>> It's too late at night for me to understand your problem, but this
>> sentence
>> sounds as if you're approaching it from the wrong angle.
>>
>> Don't think in terms of opening and closing elements. Think in terms of
>> constructing a tree of nodes. Writing an element is an atomic operation,
>> it
>> isn't two separate operations to write the start and end tags. Draw the
>> result tree you want to produce on a scrap of paper - as a tree - and keep
>> it in front of you while coding; don't think about the serialized XML
>> output.
>
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>  Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================

Current Thread