1

In the .xml file, I have like this:

<function>true</function>

In the schema ile, I have defined it as a boolean. So now, It is working correctly. BUT for XSLT file i.e .xsl,

2
  • Can you add an example xsl describing what you want to do? Commented Nov 21, 2010 at 10:59
  • Good question, +1. See my answer for a complete and short solution that doesn't use any XSLT conditional instructions. Also wait for an even shorter hack, coming ... :) Commented Nov 21, 2010 at 17:31

2 Answers 2

3

You can use xsl:choose:

<td>
  <xsl:choose>
    <xsl:when test="function = 'true'">@</xsl:when>
    <xsl:otherwise>&#32;</xsl:otherwise>
  </xsl:choose>
</td>
Sign up to request clarification or add additional context in comments.

2 Comments

@user507087 - sorry, it supposed to be the entity for space (&#32;). I missed out the #.
That space entity will be remove from the stylesheet.
0

This can be done very simply, not at all requiring conditional XSLT instructions, and completely in the spirit of the XSLT (push-style):

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="function/text()[.='true']">@</xsl:template>
 <xsl:template match="function/text()[not(.='true')]">
   <xsl:text> </xsl:text>
 </xsl:template>

 <xsl:template match="function">
  <td><xsl:apply-templates/></td>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<function>true</function>

produces the wanted, correct result:

<td>@</td>

When the same transformation is applied on the following XML document:

<function>false</function>

again the correct, wanted result is produced:

<td> </td>

Finally, using a hack (in XSLT 2.0/XPath 2.0 this isn't necessary), we can use just a single template:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="function">
  <td>
   <xsl:value-of select=
   "concat(substring('@', 1 div (.='true')),
           substring(' ', 1 div not(.='true'))
          )
   "/>
   </td>
 </xsl:template>
</xsl:stylesheet>

2 Comments

I think using conditionals is better than having to write the condition twice.
@svick: The alternative you think is better has much lower (probably close to zero) composabitiy. Also, it has significantly bigger complexity and is significantly more prone to committing errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.