Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:50:01+00:00 2026-06-09T14:50:01+00:00

I need help to put an XSLT condition that work: within for-each loop that

  • 0

I need help to put an XSLT condition that work: within for-each loop that run through the question nodes; if condition on Node A works, display details in Node B.
Example of XML:

<Survey>
    <questions>
        <Number>1.0</Number>
        <Question>Was the show good?</Question>
        <Answer>Yes/No</Answer>
        <Details>N/A</Details>
    </questions>
    <questions>
        <Number>1.1</Number>
        <Question>Explain why</Question>
        <Answer>N/A</Answer>
        <Details>The actors were good</Details>
    </questions>
    <questions>
        <Number>2.0</Number>
        <Question>Was the food good?</Question>
        <Answer>Yes|No</Answer>
        <Details>N/A</Details>
    </questions>
    <questions>
        <Number>2.1</Number>
        <Question>Provide details</Question>
        <Answer>N/A</Answer>
        <Details>The pasta was too salty</Details>
    </questions>
</Survey>

What I need is to use the loop of for-each
only If Question Number = 1.0 and Answer = ‘Yes’, display details in Question Number 1.1
only If Question Number = 2.0 and Answer = ‘No’, display details in Question Number 2.1

I have tried all the ways such as if, for each, choose/when, but it has not worked.
I have checked your other posts but could not find anything similar. Thank you kindly, tubi.

I am sorry, so it should be like this:

    <Survey>     
        <questions>
            <Number>1.0</Number>
            <Question>Was the show good?</Question>
            <Answer>Yes</Answer>
            <Details>N/A</Details>     
    </questions>     
    <questions>
         <Number>1.1</Number>
         <Question>Explain why</Question>
         <Answer>N/A</Answer>         
         <Details>The actors were good</Details>
     </questions>     
     <questions>
         <Number>2.0</Number>
         <Question>Was the food good?</Question>         
         <Answer>Yes</Answer>
         <Details>N/A</Details>
     </questions>
     <questions>
         <Number>2.1</Number>
         <Question>Provide details</Question>
         <Answer>N/A</Answer>
         <Details>The pasta was too salty</Details>
     </questions>
 </Survey> 

and Here is the piece of my code: (sorry, it is actually someoneelse’s code. I need to modify the part that display the next question when the previous one is specifically answered “Yes” or “No”

<fo:table-body start-indent="0pt">
    <xsl:for-each select="../Survey">
        <xsl:for-each select="questions">
        <xsl:sort select="Number" data-type="number" order="ascending"/>
            <fo:table-row>
                <xsl:choose>
                    <xsl:when test="ends-with(Number,'0')">
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                       <fo:block text-align="right">
                                <xsl:for-each select="Number"> ...
                           </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                            <fo:block text-align="left">
                                <xsl:for-each select="Description"> ...
                                </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center"> 
                            <fo:block text-align="left" color="blue">
                               <xsl:for-each select="Answer"> ...
                               </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>                                   </xsl:when>  
                    <xsl:when test="Number='1.1'">
                        <xsl:if test="//questions/Number='1.0' and //questions/Answer='No'">
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                               <fo:block text-align="right">
                                    <xsl:for-each select="Number"> ...
                                    </xsl:for-each>
                               </fo:block>
                           </fo:table-cell>
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                               <fo:block text-align="left">
                                   <xsl:for-each select="Question">  .....
                                   </xsl:for-each>
                               </fo:block>
                           </fo:table-cell>
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center"> 
                              <fo:block text-align="left" color="blue">  ....
                                   <xsl:for-each select="Details">
                                   </xsl:for-each>
                              </fo:block>
                           </fo:table-cell>                                      </xsl:if>  
                       </xsl:when>  
                       <xsl:otherwise>do something</xsl:otherwise>                        </xsl:choose>
               </fo:table-row>
            </xsl:if>
        </xsl:for-each>
    </xsl:for-each>
</fo:table-body>            
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:50:03+00:00Added an answer on June 9, 2026 at 2:50 pm

    As a rule of thumb. If your code contains a lot of duplication, you’re doing it wrong. XSLT is no exception.

    <!-- index followup questions (no zero after the dot) -->
    <xsl:key 
      name="kFollowupQuestions"
      match="questions[number(substring-after(Number, '.')) != 0]" 
      use="number(substring-before(Number, '.'))"
    />
    
    <!-- the <Survey> becomes the <fo:table-body> -->
    <xsl:template match="Survey">
      <fo:table-body start-indent="0pt">
        <!-- only select main questions (with a zero after the dot) -->
        <xsl:apply-templates select="questions[number(substring-after(Number, '.')) = 0]">
          <xsl:sort select="Number" data-type="number" order="ascending" />
        </xsl:apply-templates>
      </fo:table-body>
    </xsl:template>
    
    <!-- each <questions> becomes a <fo:table-row> -->
    <xsl:template match="questions">
      <xsl:variable name="qNum"   select="number(substring-before(Number, '.'))" />
      <xsl:variable name="subNum" select="number(substring-after(Number, '.'))" />
      <fo:table-row>
        <xsl:apply-templates select="Number" />
        <xsl:apply-templates select="Question" />
        <xsl:apply-templates select="Answer" />
        <xsl:apply-templates select="Details" />
      </fo:table-row>
      <!-- for main question answered with 'Yes', display followup-questions --> 
      <xsl:if test="Answer = 'Yes' and $subNum = 0">
        <xsl:apply-templates select="key('kFollowupQuestions', $qNum)">
          <xsl:sort select="Number" data-type="number" order="ascending" />
        </xsl:apply-templates>
      </xsl:if>
    </xsl:template>
    
    <!-- the <questions> children become <fo:table-cell>s -->
    <xsl:template match="questions/*">
      <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
        <fo:block text-align="right">
          <xsl:value-of select="." /><!-- or whatever -->
        </fo:block>
      </fo:table-cell>
    </xsl:template>
    

    http://www.xmlplayground.com/K4VHM2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Kind of need help understanding what this code actually outputs. Does it out put
I need to put a custom hover menu from http://www.addthis.com/help/toolbox , but Rails uses
Need help with a query that I wrote: I have three tables Company id
I need help with my WebView...I put this code in my viewDidLoad and it
I have a coding/maths problem that I need help translating into C#. It's a
i really need help, i can't see something that is obvious, and usually then
I'm trying to run a Random method inside a Thread. I need to put
I need help with the segmented control. I have put it in my window,
I need help with accessing class from Form. So I'll put my code so
i need help . its work ok in firefox or Opera. the problem is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.