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

    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:
    	<answer right="right">5</answer>
    XSL snippet, with problem area in bold:
    <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>
    It appears that the only way to make it work is to add a child node to answer in the XML file:
    	<answer right="right"><answerText>5</answerText></answer>
    ...and invoke it with this XSL:
    <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
    The City of Roses
    Instead of

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


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

  3. #3
    Join Date
    May 2008
    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