[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: xsl:if to separate child elements

Subject: Re: xsl:if to separate child elements
From: "John E. Simpson" <simpson@xxxxxxxxxxx>
Date: Tue, 26 Sep 2000 00:44:44 -0400
xsl if example child
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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.