【发布时间】:2012-08-07 23:41:01
【问题描述】:
我需要帮助来设置一个有效的 XSLT 条件:在贯穿问题节点的 for-each 循环中;如果节点 A 上的条件有效,则在节点 B 中显示详细信息。 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>
我需要的是使用 for-each 的循环 仅当问题编号 = 1.0 且答案 =“是”时,在问题编号 1.1 中显示详细信息 仅当问题编号 = 2.0 且答案 =“否”时,在问题编号 2.1 中显示详细信息
我已经尝试了所有方法,例如 if,for each,choose/when,但没有奏效。 我检查了你的其他帖子,但找不到类似的东西。谢谢你,tubi。
对不起,应该是这样的:
<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>
这是我的一段代码:(对不起,这实际上是别人的代码。当上一个问题具体回答“是”或“否”时,我需要修改显示下一个问题的部分
<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>
【问题讨论】:
-
是否允许在 XML 的同一字段中提供多个答案?我注意到你有一些说“是/否”和“是|否”。
-
请给出所需输出的示例。这将消除需求的歧义。此外,如果您在句子之间加上句号,您的“我需要什么”段落将不会那么混乱。
-
如果您展示您尝试过的一种方法,然后描述它在什么方面不起作用(实际发生了什么以及它与您想要的有什么不同),也会有所帮助。