gpt4 book ai didi

xml - 使用 XSLT 删除第一个

标签

转载 作者:数据小太阳 更新时间:2023-10-29 02:27:06 25 4
gpt4 key购买 nike

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

您的帮助将真正帮助我。提前致谢

最佳答案

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

如果您使用模板匹配而不是 for-each,此任务将非常简单.还要在 XSL 中使用正确的 namespace 声明,而不是使用 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>

关于xml - 使用 XSLT 删除第一个 <p> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13507437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com