XSLT 应用模板递归性的厄运!

发布于 2024-11-06 11:30:55 字数 1675 浏览 0 评论 0原文

我必须遵循 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-13 11:30:55

不确定您最终想要做什么,但这可能对您有帮助。

<xsl:template match="question">
  <xsl:value-of select="text"/>: 
  <select>
     <xsl:variable name="option_set_id" select="@option_set"/>
     <xsl:apply-templates select="option | //option_set[@id=$option_set_id]/option"/>
  </select>
</xsl:template>

<xsl:template match="option">
   <option>
      <xsl:value-of select="."/>
   </option>
</xsl:template>

有一些调整,例如添加上面的密钥,并检查未使用的 option_sets 等,但这将帮助您开始。

Not sure what you're ultimately trying to do, but this might help you.

<xsl:template match="question">
  <xsl:value-of select="text"/>: 
  <select>
     <xsl:variable name="option_set_id" select="@option_set"/>
     <xsl:apply-templates select="option | //option_set[@id=$option_set_id]/option"/>
  </select>
</xsl:template>

<xsl:template match="option">
   <option>
      <xsl:value-of select="."/>
   </option>
</xsl:template>

There are tweaks, like adding the key above, and checking for unused option_sets etc. but this'll get you started.

怎会甘心 2024-11-13 11:30:55
<xsl:key name="kOptionSet" match="option_set" use="@id" />

<xsl:template match="question">
  <xsl:copy>
    <xsl:copy-of select="text" />
    <xsl:copy-of select="key('kOptionSet', @option_set)/option" />
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

应该几乎可以做你想做的事。我不确定你为什么要递归。

<xsl:key name="kOptionSet" match="option_set" use="@id" />

<xsl:template match="question">
  <xsl:copy>
    <xsl:copy-of select="text" />
    <xsl:copy-of select="key('kOptionSet', @option_set)/option" />
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

Should pretty much do what you want. I'm not sure why you are recursing in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文