XSLT 应用模板递归性的厄运!
我必须遵循 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。
如果我尝试
那么我就会陷入无限循环。
I have to following XML document structure:
<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>
In the interests of keeping things DRY, the idea is to have a number of different questions which share common sets of options. These can then be built using XSLT. My templates are as follows:
<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>
The idea is that once the top template has matched my question, I will be left with something that looks like the following:
<question>
<text>Do you like cake?</text>
<option>Yes</option>
<option>No</option>
<option>Maybe</option>
</question>
... Which can then be matched by the bottom template and put into my HTML document. My question is how do I create the (top) template that actually does that. I'm close, but this still isn't working:
<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>
The transformed question block, along with its options is being copied over to the document, instead of being picked up by the top template and made into pretty HTML.
If I try to <xsl:apply-templates select="."/>
then I get caught in an infinite loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定您最终想要做什么,但这可能对您有帮助。
有一些调整,例如添加上面的密钥,并检查未使用的 option_sets 等,但这将帮助您开始。
Not sure what you're ultimately trying to do, but this might help you.
There are tweaks, like adding the key above, and checking for unused option_sets etc. but this'll get you started.
应该几乎可以做你想做的事。我不确定你为什么要递归。
Should pretty much do what you want. I'm not sure why you are recursing in the first place.