简体   繁体   English

XSLT for-each中的动态变量

[英]Dynamic variable in XSLT for-each

I want to take the question and only its options (those with their code attribute beginning with the question code). 我只想回答问题及其选项(那些问题代码以问题代码开头的选项)。 For example: if I've got a question T1Q1, I want to retrieve only the options T1Q1_1, T1Q1_2, and so on. 例如:如果我有一个问题T1Q1,我只想检索选项T1Q1_1,T1Q1_2,依此类推。

I made a for-each loop. 我做了一个for-each循环。 I wanted to iterate over every question, take its code, and then iterate again on all the options whose code begins with the question code characters. 我想遍历每个问题,获取其代码,然后再次遍历所有以问题代码字符开头的选项。 But I don't know how to do that. 但是我不知道该怎么做。

XML: XML:

<question code="T1Q1">Which encoding did you use?</question>
<option code="T1Q1_1">ISO-8859-1</option>
<option code="T1Q1_2">UTF-8</option>
<option code="T1Q1_3">Other</option>

<question code="T1Q2">Which blabla did you blabla?</question>
<option code="T1Q2_1">Absurdanipal</option>
<option code="T1Q2_2">Fantasmagoric</option>
<option code="T1Q2_3">Other</option>

XSLT: XSLT:

<xsl:for-each select="pollns:question">
<xsl:value-of select="@code"/><br/>
  <xsl:for-each select="following-sibling::pollns:option[starts-with(@code,'T1Q1')]">
    <xsl:value-of select="."/><br />
  </xsl:for-each>
</xsl:for-each>

As you can see, I wrote starts-with(@code, 'T1Q1') . 如您所见,我编写了starts-with(@code, 'T1Q1') I want to replace that T1Q1 with a dynamic variable or something similar that changes for each question. 我想用一个动态变量或类似的东西替换那个T1Q1,每个问题都会改变。

How can I do that? 我怎样才能做到这一点?

I think I've got the solution. 我想我已经找到了解决方案。

XSLT: XSLT:

<xsl:for-each select="pollns:question">
  <xsl:variable name="questionCode" select="@code"/>
  <xsl:value-of select="@code"/><br/>
     <xsl:for-each select="following-sibling::pollns:option[starts-with(@code,$questionCode)]">
       <xsl:value-of select="."/><br />
     </xsl:for-each>
</xsl:for-each>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM