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>
As a rule of thumb. If your code contains a lot of duplication, you’re doing it wrong. XSLT is no exception.
http://www.xmlplayground.com/K4VHM2