2

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>

物事を DRY に保つために、共通のオプション セットを共有するいくつかの異なる質問を用意することが考えられます。これらは、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="."/>、無限ループに陥ります。

4

2 に答える 2

2

最終的に何をしようとしているのかわかりませんが、これは役立つかもしれません。

<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をチェックしたりするなどの調整がありますが、これで開始できます。

于 2011-05-12T21:14:56.693 に答える
2
<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>

あなたが望むことをほとんど行うべきです。そもそもなぜ再帰しているのかわかりません。

于 2011-05-12T20:54:31.217 に答える