This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: xsl:if to separate child elements
- To: xsl-list at mulberrytech dot com
- Subject: Re: xsl:if to separate child elements
- From: "John E. Simpson" <simpson at polaris dot net>
- Date: Tue, 26 Sep 2000 00:44:44 -0400
- Reply-To: xsl-list at mulberrytech dot com
At 06:35 PM 09/25/2000 -0700, pmitra wrote:
> I am somehow really stuck trying to separate out different child
>elements using <xsl:if>. Please help with the following situation if
>possible:
>
>Here is an example of the type of xml I am working with - 1 help element
>with multiple heltext and helpexample elements:
>
><category name="science">
><help>
><helptext>
>This is a descriptive note
></helptext>
><helpexample>
>This is an example
></helpexample>
><helptext>
>This is another line of text
></helptext>
></help>
></category>
>
>I would like to do a for-each to be able to print out all helptext and
>helpexample elements in sequence, but I would like to give a different
>style to helptext vs helpexample (<pre></pre>).
When you say "in sequence," you mean in the order they appear in the XML
document, right? If not, please explain. If so, please read on.
Forget the for-each. Like Chris Bayes (I think) says, "Newbie rule #1:
Don't use xsl:for-each." (It often doesn't do what newcomers might think it
does.) For that matter, you don't need any xsl:if's either.
This stylesheet:
<!-- Template for root <help> element -->
<xsl:template match="help">
<!-- Process all children of <help>, i.e. all
<helptext> and <helpexample> elems., in doc order -->
<xsl:apply-templates />
</xsl:template>
<!-- Template for <helptext> elements -->
<xsl:template match="helptext">
<!-- Wrap content in <pre> element -->
<pre><xsl:value-of select="." /></pre>
</xsl:template>
<!-- Template for <helpexample> element -->
<xsl:template match="helpexample">
<!-- Just output content -->
<xsl:value-of select="." />
</xsl:template>
... when applied to your sample XML with Saxon or XT, transforms it to:
<pre xmlns="http://www.w3.org/TR/REC-html40">
This is a descriptive note
</pre>
This is an example
<pre xmlns="http://www.w3.org/TR/REC-html40">
This is another line of text
</pre>
When viewed in IE5, either this HTML output from Saxon/xt *or* the XML when
linked to the stylesheet, displays:
This is a descriptive note <--displays in fixed font
This is an example <--displays in proportional font
This is another line of text <--displays in fixed font
Is that the result you wanted?
==========================================================
John E. Simpson | "If you were going to
http://www.flixml.org | shoot a mime, would you use
XML Q&A: http://www.xml.com | a silencer?" (Steven Wright)
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list