0

I have this (pseudo-code) xml

<data>
<ref>one</ref>
<val>20</val>
<ref>two</ref>
<val>200</val>
<ref>three</ref>
<val>2000</val>
</data>

Then, let's say that I don't know which values are in "val" nodes. Thus, I would like to have the xhtml output

-> IF there is any "val" below 100

<div class="low">values 20,200,2000 in one,two,three</div>

-> IF there is any "val" below 1000 but not below 100

<div class="medium">values 20,200,2000 in one,two,three</div>

-> IF there is any "val" below 10000 but not below 1000

<div class="high">values 20,200,2000 in one,two,three</div>

*In the last 2 cases, the initial values should be different

Any idea? Thanks :)

3
  • What do you mean by: "*In the last 2 cases, the initial values should be different" ? Please, explain -- what do you mean by "initial values" and what these should be? Commented May 23, 2013 at 3:34
  • So, following the stated requirements, all three <div> elements must be output? Is that right? Commented May 23, 2013 at 4:13
  • Just state that in the "medium" case, values should be for example 120, 200, 2000, and not the original 20, 200, 2000. And the same with the "high" case. Otherwise it will find always a value below 100. Commented May 23, 2013 at 7:24

1 Answer 1

1

To get max valu in "1.0" XSLT version we need extension function. I have created it for you:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:math="http://exslt.org/math" exclude-result-prefixes="math">

  <xsl:param name="checkValue">
    <xsl:variable name="value" select="math:max(//val)"/>
    <xsl:choose>
      <xsl:when test="$value &lt; 100">
        <xsl:text>low</xsl:text>
      </xsl:when>
      <xsl:when test="$value &lt; 1000 and $value &gt; 100">
        <xsl:text>medium</xsl:text>
      </xsl:when>
      <xsl:when test="$value &lt; 10000 and $value &gt; 1000">
        <xsl:text>high</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:param>

  <xsl:template match="data">
    <div class="{$checkValue}">
      <xsl:text>values </xsl:text>
      <xsl:for-each select="val">
        <xsl:value-of select="."/>
        <xsl:if test="position()!=last()">, </xsl:if>
      </xsl:for-each>
      <xsl:text> in </xsl:text>
      <xsl:for-each select="val">
        <xsl:value-of select="preceding-sibling::ref[1]"/>
        <xsl:if test="position()!=last()">, </xsl:if>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>

output:

<div class="high">values 20, 200, 2000 in one, two, three</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic :) That's what I wanted to get

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.