3

我正在尝试使用 XSLT 将多项选择题类型的 QTI 标准 xml 转换为 XHTML 文件。我发现从“simpleChoice”标签值中删除第一个“p”标签有困难。

以下是我试图转换为 XHTML 的 QTI

    <?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>ChoiceA</value>
    </correctResponse>
  </responseDeclaration>
  <outcomeDeclaration identifier="SCORE" cardinality="single" baseType="integer">
    <defaultValue>
      <value>0</value>
    </defaultValue>
  </outcomeDeclaration>
  <itemBody>
    <div id="item">
      <div id="instruction">Select the correct options</div>
      <choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
        <prompt>
            <div id="stem">
                <p>Question will appear here</p>
            </div>
        </prompt>
        <simpleChoice identifier="ChoiceA"><p> answer's first P tag</p> <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</simpleChoice>
        <simpleChoice identifier="ChoiceB"><p> answer's first P tag</p> <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</simpleChoice>
        <simpleChoice identifier="ChoiceC"><p> answer's first P tag</p> <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</simpleChoice>
        <simpleChoice identifier="ChoiceD"><p> answer's first P tag</p> <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</simpleChoice>
        <simpleChoice identifier="ChoiceE"><p> answer's first P tag</p> <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</simpleChoice>
      </choiceInteraction>
    </div>
  </itemBody>
  <responseProcessing template="http://www.imsglobal.org/question/qti_v2p1/rptemplates/match_correct" />
</assessmentItem>

我正在查看选项的以下输出

<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="option1">
      <label>A.</label>
      <div class="optionContent">answer's first P tag<p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
    </div>          
    <div id="option2">
      <label>B.</label>
      <div class="optionContent">answer's first P tag<p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
    </div>
    <div id="option3">
      <label>C.</label>
      <div class="optionContent">answer's first P tag<p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
    </div>
    <div id="option4">
      <label>D.</label>
      <div class="optionContent">answer's first P tag<p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
    </div>
    <div id="option5">
      <label>E.</label>
      <div class="optionContent">answer's first P tag<p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
    </div>
  </body>
</html>

我需要的期望输出是从“simpleChoice”标签中删除第一个“p”标签。

我曾尝试使用以下样式来做到这一点

<xsl:for-each select="//simpleChoice[$vCurrentIndex]/*">
    <xsl:choose>
      <xsl:when test="local-name() = 'p' and position() = 1">
        <xsl:apply-templates select="node()|@*" mode="children" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="{local-name()}">
          <xsl:apply-templates select="node()|@*" mode="children" />
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

这样做后,我得到以下输出。输出不考虑任何不在 P 标签内的值。仅获取三个 P 标签的输出。

<div class="optionContent">answer's second P tag<p> answer's third P tag</p> <p> answer's forth P tag</p></div>

你的帮助真的会帮助我。提前致谢

4

2 回答 2

2

您使用 XSLT 的方法是错误的。删除<xsl:for-each>整个样式表中的所有内容。你不需要它们,也不应该使用它们。它们表明您以程序方式思考,而 XSLT 不是一种程序语言。

如果您使用模板匹配而不是for-each. 还要在 XSL 中使用正确的命名空间声明,而不是local-name()到处使用。

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:qti="http://www.imsglobal.org/xsd/imsqti_v2p1"
  exclude-result-prefixes="qti"
>
  <xsl:output indent="yes" />

  <xsl:template match="/*">
    <root>
      <xsl:apply-templates select="//qti:simpleChoice" />
    </root>
  </xsl:template>

  <!-- identity template: this is the base of the entire process -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- helper: elements in the qti namespace output their local name -->
  <xsl:template match="qti:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="node() | @*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="qti:simpleChoice">
    <div id="option{position()}">
      <label>
        <xsl:value-of select="concat(substring(@identifier, 7), '.')" />
      </label>
      <div class="optionContent">
        <xsl:apply-templates />
      </div>  
    </div>
  </xsl:template>

  <!-- the first p in a simpleChoice will just output its contents -->
  <xsl:template match="qti:simpleChoice/qti:p[1]">
    <xsl:apply-templates />
  </xsl:template>

</xsl:stylesheet>

输出(见http://www.xmlplayground.com/IiKDkY

<root>
  <div id="option1">
    <label>A.</label>
    <div class="optionContent"> answer's first P tag <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
  </div>
  <div id="option2">
    <label>B.</label>
    <div class="optionContent"> answer's first P tag <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
  </div>
  <div id="option3">
    <label>C.</label>
    <div class="optionContent"> answer's first P tag <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
  </div>
  <div id="option4">
    <label>D.</label>
    <div class="optionContent"> answer's first P tag <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
  </div>
  <div id="option5">
    <label>E.</label>
    <div class="optionContent"> answer's first P tag <p> answer's second P tag</p> <p> answer's third P tag</p> <p> answer's forth P tag</p> and text without p tag</div>
  </div>
</root>
于 2012-11-22T08:04:10.273 回答
2

我需要的期望输出是从“simpleChoice”标签中删除第一个“p”标签。

我希望我正确理解了您的问题-您想首先显示 P ,例如 DIV 或 SPAN 没有任何边距等,对吗?

那么我认为更好的方法是按<simpleChoice>...</simpleChoice>原样使用内容。但是在您的 CSS 文件中<div class="optionContent">为第一个 P 标签应用附加样式。像这样的东西:

.optionContent > p
{
    display: inline;
}
于 2012-11-22T07:44:14.557 回答