Results 1 to 2 of 2

Thread: XML with XSL

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    26

    XML with XSL

    I have the (simplified) xml:

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="Testing.xsl"?>
    <questionset>
      <question>
        <stem>Question 1.a</stem><answer>1</answer>
        <stem>Question 1.b</stem><answer>2</answer>
        <stem>Question 1.c</stem><answer>3</answer>
        <stem>Question 1.d</stem><answer>4</answer>
      </question>
      <question>
        <stem>Question 2.a</stem><answer>5</answer>
        <stem>Question 2.b</stem><answer>6</answer>
        <stem>Question 2.c</stem><answer>7</answer>
      </question>
    </questionset>
    Being transformed by the XSLT:

    Code:
    <xsl:for-each select="questionset/question/stem">
      <xsl:variable name="qnum">
        <xsl:value-of select="position()"/>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="self::stem">
          <xsl:for-each select="../answer">
            <xsl:variable name="zero" select="0"/>
            <xsl:variable name="cor_count">
              <xsl:value-of select="count(../answer)"/>
            </xsl:variable>
            <xsl:variable name="index">
              <xsl:value-of select="position()"/>
            </xsl:variable>
            <xsl:variable name="difference">
              <xsl:value-of select="$qnum mod $cor_count"/>
            </xsl:variable>
    <div>
      <xsl:if test="($index = $difference) or ($index = $cor_count and $difference = $zero)">
        Answer_<xsl:value-of select="$qnum"/> = '<xsl:value-of select="."/>';
      </xsl:if>
    </div>
          </xsl:for-each>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
    The problem is that the answers come out of order... why is this???
    If I add a fourth question to the second questionset above in the XML, then it comes out right. I'm confused???

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    26

    Re: XML with XSL

    I've made my own solution with the following xsl:

    Code:
    <xsl:for-each select="questionset/question/stem">
      <xsl:variable name="qnum">
        <xsl:value-of select="position()"/>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="self::stem">
    	  <xsl:variable name="index">
    	    <xsl:value-of select="position()"/>
    	  </xsl:variable>
      	  <xsl:variable name="first">
    	    <xsl:value-of select="1"/>
    	  </xsl:variable>
          <xsl:for-each select="following::answer">
    <div>
        <xsl:if test="($index = $qnum) and (position() = $first)">
          Answer_<xsl:value-of select="$qnum"/> = '<xsl:value-of select="."/>';
        </xsl:if>
    </div>
          </xsl:for-each>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
    Yes, I realize this is rather messy code, but I am trapped into having to utilize the xml structure AS IS and I must use the parent for-each select as provided (I can't change it) because of other programs that utilize this code. Ahhh, the joys of working somewhere where people don't program...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width