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

时间:2016-01-11 20:25:36

标签: xml xslt xslt-1.0

我有以下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来获取Response的值:我需要进一步查询它并根据它输出不同的值。

我试过这个:

<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 个答案:

答案 0 :(得分:2)

似乎你正在使它变得比它需要的更复杂。如果您想测试对问题#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(您还没有回答我的问题)。否则,我们需要如何输出多个结果的说明。

答案 1 :(得分:0)

我箍这会帮助你:

编辑: 我已经为此添加了一些解决方案:

<xsl:template match="/jobDetails">
  <Output>
    <Best>
      <xsl:choose>
        <xsl:when test="Question[QuestionNumber = '2']/QuestionResponse[Response = 'AnotherValue']">
          <Response>This is for you</Response>
        </xsl:when>
      </xsl:choose>
    </Best>
    <Looping>
      <xsl:for-each select="./Question">
        <Var>
          <!-- This is the original statement from you! Watch the output of it! -->
          <xsl:value-of select="QuestionNumber[string(.)='2']"/>
        </Var>
        <!-- Using the choose statement! -->
        <xsl:choose>
          <!-- You should list the ones you want to output -->
          <xsl:when test="QuestionNumber = 1">
            <Response>Just Some Value</Response>
          </xsl:when>
          <xsl:when test="QuestionNumber = 2">
            <Response>We found an interesting QuestionNumber</Response>
          </xsl:when>
          <xsl:otherwise>
            <Response>More Value</Response>
          </xsl:otherwise>
        </xsl:choose>

        <ApplyTemplate>
          <!-- We can use template for this too-->
          <xsl:apply-templates select ="."/>
        </ApplyTemplate>

      </xsl:for-each>
    </Looping>
  </Output>
</xsl:template>

<xsl:template match="Question[QuestionNumber = 2]">
  <ProcessTemplate>
    <!-- This is also perfect if you want to do more staff here-->
    <xsl:choose>
      <!-- You should list the ones you want to output -->
      <xsl:when test="QuestionResponse/Response = 'AnotherValue'">
        <Response>This is for you too</Response>
      </xsl:when>
      <xsl:when test="QuestionResponse/Response = 'SomeValue'">
        <Response>You wont graduate</Response>
      </xsl:when>
    </xsl:choose>
  </ProcessTemplate>
</xsl:template>

<xsl:template match="Question">
  <!-- This is empty for the case we did not match -->
  <Response>We don't have rule for this</Response>
</xsl:template>

这将输出以下xml:

<Output>
  <Best>
    <Response>This is for you</Response>
  </Best>
  <Looping>
    <Var></Var>
    <Response>Just Some Value</Response>
    <ApplyTemplate>
      <Response>We don't have rule for this</Response>
    </ApplyTemplate>
    <Var>2</Var>
    <Response>We found an intresting QuestionNumber</Response>
    <ApplyTemplate>
      <ProcessTemplate>
        <Response>This is for you too</Response>
      </ProcessTemplate>
    </ApplyTemplate>
  </Looping>
</Output>