在 xsl:for-each 之后获取标签值

Getting tag values after xsl:for-each

我有以下 XML:

<Order>
  <jobDetails>
    <Question>
      <QuestionNumber>1</QuestionNumber>
      <QuestionResponse>
        <Response>Value</Response>
      </QuestionResponse>
    </Question>
    <Question>
      <QuestionNumber>2</QuestionNumber>
      <QuestionResponse>
        <Response>AnotherValue</Response>
      </QuestionResponse>
    </Question>
  </jobDetails>
</Order>

如果问题编号为 2,我正在使用 XSLT 1.0 获取响应的值:我需要进一步查询它并根据它输出不同的值。

我试过这个:

<xsl:template match="Order">
  <xsl:for-each select="jobDetails/Question">
    <xsl:variable name="theSet" select="QuestionNumber[string(.)='2']" />
    <xsl:if test="$theSet">
      <xsl:when test="QuestionResponse/Response = 'Value'"><Response>SomeValue</Response></xsl:when> 
      <xsl:when test="QuestionResponse/Response = 'AnotherValue'"><Response>SomeOtherValue</Response></xsl:when>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

...但 XSLT 未验证。

在此先感谢您的帮助。

你好像把事情搞得比实际情况更复杂了。如果您想测试对问题 #2 的回答,您可以直接这样做,而无需完成所有其他问题:

<xsl:template match="/Order">
    <xsl:variable name="resp2" select="jobDetails/Question[QuestionNumber='2']/QuestionResponse/Response" />
    <response>
        <xsl:choose>
            <xsl:when test="$resp2='Value'">SomeValue</xsl:when>
            <xsl:when test="$resp2='AnotherValue'">SomeOtherValue</xsl:when>
        </xsl:choose>
    </response>
</xsl:template>

请注意,这假设整个 XML 中只有一个问题 #2(您还没有回答我的问题)。否则我们需要说明如何输出多个结果。