[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] how to match true and false and display yes and no instead?


Subject: Re: [xsl] how to match true and false and display yes and no instead?
From: JBryant@xxxxxxxxx
Date: Wed, 27 Jul 2005 13:57:26 -0500

Just a couple quick observations that'll help you as you learn more about 
XSLT.

Rather than

<xsl:apply-templates select="profile/kid"></xsl:apply-templates>

you can do 

<xsl:apply-templates select="profile/kid"/>

which will save you some typing.  There are times when you want to 
separate them (so that you can slip a sort instruction in there, for 
example), but, if not doing something like that, the simple way works.

Other than that, you could simplify your apply-templates instructions a 
bit by having a template for profile. Then the templates would look like 
this:

<xsl:template match="members">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="profile">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="married">
  <xsl:if test=".='false'">no</xsl:if>
  <xsl:if test=".='true'">yes</xsl:if>
</xsl:template>

<xsl:template match="kid">
  <xsl:apply-templates select="../married">
</xsl:template>

There's nothing wrong with the way you did it. I simply offer another way 
(the one I generally use unless the source dictates something more 
elaborate) as something to consider.

Welcome to XSL and to the bestest list on the Internet.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





Christian Rasmussen <byggemandbob@xxxxxxxxx> 
07/27/2005 12:49 PM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
Re: [xsl] how to match true and false and display yes and no instead?






Of course you are right... I was too fast making a sample xml. The
real xml-file I'm working on is too complex to post here, so its
better with a sample.

However, I've made a better sample and actually now it works: Here's
the solution:

do you have any comments to this? 
thank-you for respons anyway...

XML-FILE:
=======
<?xml version="1.0" encoding="UTF-8"?>
<members>
                 <profile>
                                 <name>Bill Clinton</name>
                                 <married>false</married>
                                 <kid>joe</kid>
                 </profile>
</members>


XSL-FILE:
=======
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="members">
members-template is matched!
<xsl:apply-templates select="profile/kid"></xsl:apply-templates>
</xsl:template>

<xsl:template match="members/profile/married">
<xsl:if test=".='false'">no</xsl:if>
<xsl:if test=".='true'">yes</xsl:if>
</xsl:template>

<xsl:template match="kid">
<xsl:apply-templates select="../married"></xsl:apply-templates>
kids-template is matched!
</xsl:template>

</xsl:stylesheet>









On 7/27/05, JBryant@xxxxxxxxx <JBryant@xxxxxxxxx> wrote:
> Well, if you actually tried to run these templates, your XML parser 
should
> spit out error messages.
> 
> You seem to be trying to use </xsl:apply-templates> to close
> <xsl:template>, which isn't XML.
> 
> Assuming you mean
> 
> <xsl:template match="members">
> xsl-fo goes here.....
> </xsl:template>
> 
> then you want
> 
> <xsl:template match="members">
> xsl-fo goes here.....
> <xsl:apply-templates/>
> </xsl:template>
> 
> Otherwise, the processor gets to members and stops, never processing the
> children of members.
> 
> Jay Bryant
> Bryant Communication Services
> (presently consulting at Synergistic Solution Technologies)
> 
> 
> 
> 
> Christian Rasmussen <byggemandbob@xxxxxxxxx>
> 07/27/2005 11:57 AM
> Please respond to
> xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> 
> 
> To
> xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> cc
> 
> Subject
> [xsl] how to match true and false and display yes and no instead?
> 
> 
> 
> 
> 
> 
> Hi experts,
> 
> It seems to be a pretty simple question, but I'm newbie and I cannot
> figure out how to do it :-(
> please help me!
> 
> here is some sample xml showing my problem:
> 
> <members>
>                  <profile>
>                                  <name>Bill Clinton</name>
>                                  <married>false</married>
>                                  <kid>joe</kid>
>                  </profile>
> </members>
> 
> 
> my xsl looks something like this:
> 
> <xsl:template match="members">
> xsl-fo goes here.....
> </xsl:apply-templates>
> 
> <xsl:template match="members/profile/married">
> <xsl:if test=".='false'">no, he is not married</xsl:if>
> <xsl:if test=".='true'">yes, he is married</xsl:if>
> </xsl:apply-templates>
> 
> <xsl:template match="kid">
> xsl-fo goes here.....
> <xsl:apply-templates select="../married"></xsl:apply-templates>
> 
> I simply want to display "yes" and "no" instead of "true" and "false".
> So I have made a template match which specifically matches the element
> which holds the true or false value. After matching this element, I
> test whether its true or false, and returns the text instead.
> 
> nothing shows up :-(
> 
> Thanx in advance for you help
> /Christian


Current Thread
Keywords