[xsl] [xslt] Problem changing a contained element to a following element

Subject: [xsl] [xslt] Problem changing a contained element to a following element
From: <email@xxxxxxxx>
Date: Fri, 27 Jun 2003 14:23:34 -0500
Hi,

I didn't think of myself as an XSLT newbie, but this problem sure is making
me feel like one.

All I want to do is to change:

<p>some text
    <ul>
      <li><p>item1</p>
      <li><p>item1</p>
   </ul>
</p>

to:

<p>some text</p>
<ul>
   <li><p>item1</p>
   <li><p>item1</p>
</ul>

I don't want <p> to contain <ul>.  The <p> must end before the <ul> begins.

I run the following with Xalan Java 2.2.0:

<?xml version="1.0"?>

<xsl:transform
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
		xmlns:xalan="http://xml.apache.org/xslt";
		version="1.0">

<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="contents">

	<contents-corrected>
		<xsl:apply-templates/>
	</contents-corrected>

</xsl:template>

<xsl:template match="p">

	<p-unchanged>
		<xsl:apply-templates/>
	</p-unchanged>

</xsl:template>


<!--
*
*	A paragraph that contains an unordered list should be changed into a
paragraph
*	*followed* by an unordered list.
*
-->

<xsl:template match="p[child::ul]">

	<p-corrected>
		<xsl:apply-templates select="node()[not(ul)]"/>
	</p-corrected>

	<xsl:apply-templates select="ul"/>

</xsl:template>


<xsl:template match="ul | li">

	<xsl:copy-of select="."/>

</xsl:template>

</xsl:transform>

With this input XML:

<?xml version="1.0" encoding="UTF-8"?>
<snippet>
	<contents>
		<p>The likelihood of achieving an accumulation goal is affected by these
factors&#58;
			<ul>
				<li>
					<p>Inflation of goal&#45;related costs</p>
				</li>
				<li>
					<p>The original amount invested</p>
				</li>
				<li>
					<p>The rate of return on invested assets</p>
				</li>
			</ul>
		</p>
		<p>Consequently, a calculation showing the need for additional resources
to achieve your accumulation goal will be directly affected by changes in
any of these factors.
		</p>
	</contents>
</snippet>

And I get this result:

<?xml version="1.0" encoding="UTF-8"?>

	<contents-corrected>
		<p-corrected>The likelihood of achieving an accumulation goal is affected
by these factors:
			<ul>
				<li>
					<p>Inflation of goal-related costs</p>
				</li>
				<li>
					<p>The original amount invested</p>
				</li>
				<li>
					<p>The rate of return on invested assets</p>
				</li>
			</ul>
		</p-corrected><ul>
				<li>
					<p>Inflation of goal-related costs</p>
				</li>
				<li>
					<p>The original amount invested</p>
				</li>
				<li>
					<p>The rate of return on invested assets</p>
				</li>
			</ul>
		<p-unchanged>Consequently, a calculation showing the need for additional
resources to achieve your accumulation goal will be directly affected by
changes in any of these factors.
		</p-unchanged>
	</contents-corrected>


I am confounded as to why the <ul> comes out twice due to this template:


<xsl:template match="p[child::ul]">

	<p-corrected>
		<xsl:apply-templates select="node()[not(ul)]"/>
	</p-corrected>

	<xsl:apply-templates select="ul"/>

</xsl:template>

I'm hoping I'm missing something obvious and that someone here can find it
easily.  Thanks!

. . . Phil





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread