我必须遵循 XML 文档结构:
<option_set id="1">
<option>Yes</option>
<option>No</option>
<option>Maybe</option>
</option_set>
<question option_set="1">
<text>Do you like cake?</text>
</question>
<question option_set="1">
<text>Is the cake a lie?</text>
</question>
为了让事情保持干燥,这个想法是有许多不同的问题,它们共享共同的选项集。然后可以使用 XSLT 构建这些。我的模板如下:
<xsl:template match="question[@option_set and not(option)]">
<!-- Build a whole question with its options
(copy the options across and then apply-templates?) -->
</xsl:template>
<xsl:template match="question[option]">
<!-- Match a whole question, with options, for making pretty HTML out of -->
</xsl:template>
这个想法是,一旦顶部模板与我的问题匹配,我将得到如下所示的内容:
<question>
<text>Do you like cake?</text>
<option>Yes</option>
<option>No</option>
<option>Maybe</option>
</question>
...然后可以与底部模板匹配并放入我的 HTML 文档中。我的问题是如何创建实际执行此操作的(顶部)模板。我很接近,但这仍然不起作用:
<xsl:template match="question[@option_set and not(option)]">
<xsl:variable name="optset" select="@option_set"/>
<xsl:copy>
<xsl:copy-of select="text"/>
<xsl:copy-of select="//option_set[@id=$optset]/option"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
转换后的问题块及其选项被复制到文档中,而不是被顶部模板拾取并制作成漂亮的 HTML。
如果我尝试这样做,<xsl:apply-templates select="."/>
我就会陷入无限循环。