Jump to content

Conditional Statement around a template


Skemcin
 Share

Recommended Posts

I have my well formated XML, and while transforming it I have something like this:

<h3>Accessories</h3>These are items you can buy that require no modifications:<br /><xsl:apply-templates select="Accessories" /><br /><br clear="all" />

In the accessories template, it successfully loops through my XML and is able to output exactly the way I want. But, when there are no Accessories for the product being loaded, how can I avoid displaying this:

<h3>Accessories</h3>These are items you can buy that require no modifications:<br /><br clear="all" />

Is there a way to wrap a condition arouns the whole template?orDo I have to move the text inside the template and create a condition that only shows it for the first record - how would that be done without using javascript?Thanks in advance.

Link to comment
Share on other sites

I don't imagine you can wrap a conditional around a template but I do think there's an answer to your problem. I'd have to see exactly how your code is setup to write a copy-paste quick fix but here's the general idea.

<xsl:template match="nodeAboveAccessories">   <xsl:choose>      <xsl:when test="Accessories>         <h3>Accessories</h3>         These are items you can buy that require no modifications:<br />         <br clear="all" />         <xsl:apply-templates select="Accessories"/>      </xsl:when>      <xsl:otherwise>         <h3>Accessories</h3>         There are no accessories for you.  Sorry.<br/>         <br clear="all"/>      </xsl:otherwise>   </xsl:choose></xsl:template>

Link to comment
Share on other sites

Maybe I didn't articulate that well. But lets say you have a category, a title, and a description. And in the template, the title and description are looping to produce all your products. So that:

<h3>Accessories</h3>These are items you can buy that require no modifications:<br /><xsl:apply-templates select="Accessories" /><br /><br clear="all" />

produces this:

<h3>Accessories</h3>These are items you can buy that require no modifications:<br />Title 1/Description1<br />Title 2/Description2<br />Title 3/Description3<br />Title 4/Description4<br />Title 5/Description5<br /><br clear="all" />

But if there are no records, I do not want to show anything. As it stands, if there are no Accessories, I get this:

<h3>Accessories</h3>These are items you can buy that require no modifications:<br />

because that is the part that is not in the template.If the template has no information to return, I simply want to not show it and not show the text just before it - because its just a label for it.Does that clarify it any more?

Link to comment
Share on other sites

That would just be an if statement if I understand you correctly. I suppose it all depends on how you want it to be organized and what things should be grouped where. The following code will do what you're asking but maybe not in the way that you're asking.

<xsl:if test="Accessories">     ...your <h3> code here and apply the templates...</xsl:if>

Link to comment
Share on other sites

That would just be an if statement if I understand you correctly.  I suppose it all depends on how you want it to be organized and what things should be grouped where.  The following code will do what you're asking but maybe not in the way that you're asking.
<xsl:if test="Accessories">     ...your <h3> code here and apply the templates...</xsl:if>

That has definitely removed the code. I tried that but with actually defining the condition. I'll play with it and the query a little more to see if it works.Thanks.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...