1

現在、XSLTを使用してQTIをXHTMLに変換する作業を行っています。

私はいくつかのhtmlタグを持っているかもしれない1つのタグを持っています。現在、次の例では、次のQTIxmlを使用しています

<?xml version="1.0" encoding="utf-8"?>
<assessmentItem xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 http://www.imsglobal.org/xsd/imsqti_v2p1.xsd" identifier="choice" title="Item Title will come here" adaptive="false" timeDependent="false" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
    <correctResponse>
      <value>D</value>
    </correctResponse>
  </responseDeclaration>
  <outcomeDeclaration identifier="SCORE" cardinality="single" baseType="integer">
    <defaultValue>
      <value>0</value>
    </defaultValue>
  </outcomeDeclaration>
  <itemBody>
    <div id="item">
      <choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
        <prompt>Question text appears here?</prompt>
        <simpleChoice identifier="A">
          <img src="a.gif" height="75px" width="75px" id="img0" alt=""></img>
        </simpleChoice>
        <simpleChoice identifier="B">
          <img src="b.gif" height="75px" width="75px" id="img1" alt=""></img>
        </simpleChoice>
        <simpleChoice identifier="C">
          <img src="c.gif" height="75px" width="75px" id="img2" alt=""></img>
        </simpleChoice>
        <simpleChoice identifier="D">
          <img src="d.gif" height="75px" width="75px" id="img3" alt=""></img>
        </simpleChoice>
        <simpleChoice identifier="E">
          <img src="e.gif" height="75px" width="75px" id="img4" alt=""></img>
        </simpleChoice>
      </choiceInteraction>
    </div>
  </itemBody>
  <responseProcessing template="http://www.imsglobal.org/question/qti_v2p1/rptemplates/match_correct" />
</assessmentItem>

simpleChoiceタグに子ノードとしてimgタグがあるかどうかを確認したいので、<div class='content'>以下のように出力タグにスタイルを追加します。simpleChoiceタグに子ノードとしてimgタグがない場合は、スタイル属性をスキップします。<div class='content'>出力タグ。

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
    <div id="wrapper">
      <div id="item">
        <div id="qtn">Question text appears here?</div>
        <div id="inx">
          <div id="ih1">
            <div id="mc1">
              <label id="l1">A.</label>
              <div class="content" style='vertical-align:middle;display:inline-block'>
                <img src="a.gif" height="75px" width="75px" id="img0" alt=""/>
              </div>
            </div>
          </div>
          <div id="ih2">
            <div id="mc2">
              <label id="l2">B.</label>
              <div class="content" style='vertical-align:middle;display:inline-block'>
                <img src="b.gif" height="75px" width="75px" id="img1" alt=""/>
              </div>
            </div>
          </div>
          <div id="ih3">
            <div id="mc3">
              <label id="l3">C.</label>
              <div class="content" style='vertical-align:middle;display:inline-block'>
                <img src="c.gif" height="75px" width="75px" id="img2" alt=""/>
              </div>
            </div>
          </div>
          <div id="ih4">
            <div id="mc4">
              <label id="l4">D.</label>
              <div class="content" style='vertical-align:middle;display:inline-block'>
                <img src="d.gif" height="75px" width="75px" id="img3" alt=""/>
              </div>
            </div>
          </div>
          <div id="ih5">
            <div id="mc5">
              <label id="l5">E.</label>
              <div class="content" style='vertical-align:middle;display:inline-block'>
                <img src="e.gif" height="75px" width="75px" id="img4" alt=""/>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
4

2 に答える 2

1

simpleChoice 要素を照合するときは、xpath にフィルターを含めてみてください。例えば、

simpleChoice      <!-- matches a simpleChoice element -->
simpleChoice[img] <!-- matches a simpleChoice element with a child img element -->

次のように、これをテンプレートで使用できます。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />

    <xsl:template match="/">
        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>
            <body>
                ((etc, etc, etc, skipping a bit here for brevity; jumping right
                into the simpleChoice items))
                <xsl:apply-templates select="//simpleChoice" />
            </body>
        </html>
    </xsl:template>

    <!-- This matches all other simpleChoice items that are not matched above. -->
    <xsl:template match="simpleChoice">
        <div class="content">
            <xsl:value-of select="@identifier" />
        </div>
    </xsl:template>

    <!-- This matches simpleChoice items with an img as a direct child -->
    <xsl:template match="simpleChoice[img]">
        <div class="content" style="your style goes here">
            <xsl:value-of select="@identifier" />
        </div>
    </xsl:template>

</xsl:stylesheet>

ライブの例はこちら.


編集

ブロックの下に深くネストされている可能性のある画像をキャッチするには、子孫simpleChoiceを検索する必要があります。これは非常に単純な変更です。私の例では、次の行を変更します。

<xsl:template match="simpleChoice[img]">

代わりにこれに:

<xsl:template match="simpleChoice[descendant::img]">

更新された例を次に示します。xpath 軸の詳細については、こちらを参照してください。

于 2012-11-27T15:14:45.597 に答える
1

<xsl:attribute>子要素を追加する前にそうする限り、リテラルの結果要素内で使用できるため、次のようなものが機能します。

<xsl:template match="simpleChoice">
  <div class="content">
    <xsl:if test="img">
      <xsl:attribute name="style">vertical-align:middle;display:inline-block</xsl:attribute>
    </xsl:if>
    <!-- rest of the div content goes here -->
  </div>
</xsl:template>
于 2012-11-27T15:21:27.257 に答える