2

I have some XML data that I want to convert to HTML with XSLT and I have mostly got it right. The problem with my XML input is that it contains inline if statements / boolean expressions that I have no idea how to handle.

<section>
  <title>My Title</title>
  <paragraph>
    <phrase class="inline-if">if_xVariable || if_yVariable</phrase>
    Show this text if if_xVariable or if_yVariable is true.
  </paragraph>
  <paragraph>
    <phrase class="inline-if">if_xVariable &amp;&amp; if_yVariable</phrase>
    Show this text if if_xVariable and if_yVariable is true
  </paragraph>
  <paragraph>
    Always show this text
  </paragraph>
</section>

I have been thinking of adding xslt-vairables with each variable name (e.g. if_xVariable) to true/false and in some way check against them.

How would you go about to solve this problem?

Update This is what I've tried

<xsl:template match="section/paragraph">
  <xsl:variable name="inlineif" select="phrase[@class='inline-if']"/>
    <xsl:if test="$inlineif">
      <p>
      <xsl:value-of select="."/>
      </p>
    </xsl:if>
</xsl:template>

Since I have neither of if_xVariable or if_yVariable specified, the output should probably be something like

<p>
Always show this text
</p>

but instead I get the output from all of the paragraphs.

Cœur
  • 37,241
  • 25
  • 195
  • 267
m__
  • 1,721
  • 1
  • 17
  • 30
  • 1
    Have a look to [this](http://stackoverflow.com/questions/4591452/evaluate-dynamic-string-as-an-xpath-expression). – H-Man2 Nov 17 '11 at 06:51
  • @H-Man2 your answer seems to be the most fitting I've encountered so far. Please add a regular answer and I will accept it. – m__ Dec 06 '11 at 08:51

3 Answers3

0

you can use following

<xsl:if test="expression">
    ...some output if the expression is true...
 </xsl:if>

full example is at http://www.w3schools.com/xsl/xsl_if.asp

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • Yes, that's true but how do I convert my if_xVariable to $if_xVariable to signal it is a variable I want to check against? And how do I convert && to &&? From what I've read, string manipulation is not xslt's strongest point. – m__ Nov 17 '11 at 06:56
  • your if_xVariable should be a tag in xml file so as youget values from xml file see the try it your self in above example in which xml file has price tag and the example uses – Hemant Metalia Nov 17 '11 at 07:02
  • I can't get it to work, and according to [this question](http://stackoverflow.com/questions/3070575/evaluate-a-string-expression-in-xsl) its not even possible without extensions. Do you have a working solution or should I accept it doesn't work? – m__ Nov 18 '11 at 07:17
0

this is the code in xsl file

in tag

  <xsl:for-each select="persons/person">
  <xsl:if test="GenderElementType='combo'">
       <select name="gender">
      <option>
      <xsl:value-of select="Gender"/>
      </option>
      <xsl:if test="Gender='MALE'">
      <option>
       FEMALE
      </option>
      </xsl:if>
      <xsl:if test="Gender='FEMALE'">
      <option>
       MALE
      </option>
      </xsl:if>
      </select>
      </xsl:if>
 </xsl:for-each>

person.xml is as follow

 <persons>
 <person>
        <Gender>MALE</Gender>
    <GenderElementType>radio</GenderElementType>


</person>

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • I don't think you've understood the question correctly. I have no problem using if statements as long as I can design them myself. Unfortunately, so is not the case this time. I've updated the original question with more info of what I've tried so far. – m__ Nov 18 '11 at 07:39
0

XSLT has no built-in dynamic evaluation of expressions. Possible solutions are discussed in the answer to this question.

Community
  • 1
  • 1
H-Man2
  • 3,169
  • 20
  • 19