www.webdeveloper.com
Results 1 to 3 of 3

Thread: XSL for-each: is there a way to get the text value of the iterated element?

  1. #1
    Join Date
    Nov 2007
    Posts
    22

    XSL for-each: is there a way to get the text value of the iterated element?

    As I'm trying to wrap my mind around XSLT, there's something that's kinda bothering me.

    XML snippet:
    Code:
    <answerList>
    	<answer>3</answer>
    	<answer>4</answer>
    	<answer right="right">5</answer>
    </answerList>
    XSL snippet, with problem area in bold:
    Code:
    <xsl:for-each select="answerList/answer">
    <xsl:if test="@right='right'">&lt;b&gt;* * *</xsl:if>
    
    <!--The selector below displays the first answer, 3, three times. I want to be able to display each answer.-->
    <xsl:value-of select="../answer"/>
    
    <xsl:if test="@right='right'">* * *&lt;/b&gt;</xsl:if>
    <br/>
    </xsl:for-each>
    It appears that the only way to make it work is to add a child node to answer in the XML file:
    Code:
    <answerList>
    	<answer><answerText>3</answerText></answer>
    	<answer><answerText>4</answerText></answer>
    	<answer right="right"><answerText>5</answerText></answer>
    </answerList>
    ...and invoke it with this XSL:
    Code:
    <xsl:for-each select="answerList/answer">
    <xsl:if test="@right='right'">&lt;b&gt;* * *</xsl:if>
    
    <xsl:value-of select="answerText"/>
    
    <xsl:if test="@right='right'">* * *&lt;/b&gt;</xsl:if>
    Do I really need to add a child node if I want to get the text out of a node where I'm using for-each? If you feel that this question reflects a lack of real understanding of XSL, you'd be right... I'm new to this and am finding it very counterintuitive.

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Instead of

    <xsl:value-of select="../answer"/>

    do

    <xsl:value-of select="."/>
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    May 2008
    Posts
    381
    Jeff is right. When you do "../answer", you're selecting the first answer element every time.

    What you're looking for is indeed to select the current node (referred to as '.') because you're already inside each answer element when you iterate via xsl:for-each. Because you're inside the answer element already, when you add a child node and select it via xsl:value-of, it works. However, you have no child nodes inside the answer element, meaning the answer element itself has contents. As a result, you would need to select the current node.

    I'm not sure if I helped or confused you more.

    It sounds like you might have a slight bit of programming experience, so I'm sure it is the for-each concept that is tripping you up (most programming languages iterate over integer ranges, not items themselves).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles