Re: [xsl] Simple Question

Subject: Re: [xsl] Simple Question
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 30 Jan 2007 22:19:52 +0100
Punnoose, Roshan wrote:
I know that this is a really simple question but I just can't seem to
figure it out. I want to print out xml if a certain condition is true,
so I thought the bottom would work, but of course it isn't valid xml.
Any ideas?

<xsl:if test="$a != $times">
<xqx:orOp>
<xqx:firstOperand>
</xsl:if>

I am under the impression that you omit a part. Is this what you are trying?


<xsl:if test="somecondition">
   <sometag>  <!-- open the tag -->
</xsl:if>

...

<xsl:if test="somecondition">
  </sometag>   <!-- close the tag -->
</xsl:if>


Which is impossible to do, as you pointed out: it is not XML. And XSLT must be XML in the first place. It seems that you are trying to instruct the processor HOW something must be achieved. This is not what you should try. Just tell the processor WHAT you want it to output by what RULES (check out www.w3schools.com for a basic tutorial on this principle, or any xslt book).


We will need to see more of your code, or better, of what you want to output, because this is too little information. Something I can think of:

<xsl:template match="somematch">
  <xsl:choose>
  <xsl:when test="$a != $times" >
      <xsl:apply-templates select="somechild" mode="create-orOp" />
  </xsl:when>
  <xsl:otherwise>
     <xsl:apply-templates select="somechild" />
  <xsl:otherwise>
  <xsl:choose>
</xsl:template>

<xsl:template match="somechild"  mode="create-orOp">
   <xqx:orOp>
       <xqx:firstOperand>
            <xsl:apply-templates select="self::somechild" />
       </xqx:firstOperand>
   </xqx:orOp>
</xsl:template>

<xsl:template match="somechild">
   ... do something ...
</xsl:template>

The above code is one way to include/exclude the orOp + firstOperand as parents from some output. But it is likely not the best way, and I can think of many scenario's where the same is possible without the xsl:choose. But that depends on what you actually are trying to achieve.

Cheers,
-- Abel Braaksma
  http://www.nuntia.nl

Current Thread