使用XSLT删除第一个<p>标记</p>

时间:2012-11-22 07:07:32

标签: xml xslt transformation

我正在尝试使用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>

你的帮助对我很有帮助。提前致谢

2 个答案:

答案 0 :(得分:2)

  

我需要的欲望输出是删除第一个'p'标签   'simpleChoice'标签。

我希望我能理解你的问题 - 你想先显示P像DIV,或者像SPAN一样没有任何游戏等等,对吗?

然后我认为更好的方法是使用<simpleChoice>...</simpleChoice>的内容。但是在<div class="optionContent">的CSS文件中为第一个P标签应用了一个额外的样式。像这样:

.optionContent > p
{
    display: inline;
}

答案 1 :(得分: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>