CDATA 有点问题。对于我当前的项目,我必须将一些 HTML 元素转换为 CDATA,以便通过 XML 导入测试用例管理系统。我现在的任务是导出该数据并将其转换为 DITA。以下是我正在使用的数据的摘录,我认为它提供了足够的上下文:
<tr:testopia><tr:testplan><tr:testcase>
<tr:text version="1">
<tr:author>bugzilla</tr:author>
<tr:action><![CDATA[<ol xmlns="http://www.w3.org/1999/xhtml">
<li>
<p xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:cln="http://clean/" class="Table">Refer to
<span style="color:red">CHM795_Workflow_DITA_Test_plan.doc
</span>
</p>
</li>
</ol>]]></tr:action>
<tr:expected_result><![CDATA[<ol xmlns="http://www.w3.org/1999/xhtml">
<li>
<p xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:cln="http://clean/"
class="Table">All requirements are met & example example</p>
</li>
</ol>]]></tr:expected_result>
<tr:setup><![CDATA[]]></tr:setup>
<tr:breakdown><![CDATA[]]></tr:breakdown>
</tr:text>
</tr:testcase></tr:testplan></tr:testopia>
为此,我需要再次取出之前保存在 CDATA 中的元素,并且我从少量研究中发现,如下所示的行将实现此目的:
<xsl:template match="tr:text">
<prerequisites>
<xsl:apply-templates select="tr:setup"/>
</prerequisites>
<postrequisites>
<xsl:apply-templates select="tr:breakdown"/>
</postrequisites>
<process>
<actions>
<xsl:apply-templates select="tr:action"/>
</actions>
<results>
<xsl:apply-templates select="tr:expected_result"/>
</results>
</process>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" disable-output-escaping="yes"/>
</xsl:template>
这将为我提供以下信息:
<case>
<prerequisites/>
<postrequisites/>
<process>
<actions><ol xmlns="http://www.w3.org/1999/xhtml"> <li> <p xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:cln="http://clean/" class="Table">Perform <span style="color:red">ASM Test Document.docx</span> </p> </li> </ol></actions>
<results><ol xmlns="http://www.w3.org/1999/xhtml"> <li> <p xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:cln="http://clean/" class="Table">Test plan successfully completed</p> </li> </ol></results>
</process>
我目前面临的问题是:禁用输出转义让我得到我需要的格式,翻译工作正常并且 DITA 文件验证。但是,在其中一些测试用例中,HTML 元素的内容有时包含诸如&
. 在此阶段之后,当我尝试将这些案例转换为最终形式时,在使用 SAXON 运行时收到如下错误:
Error reported by XML parser: The entity name must immediately follow
the '&' in the entity reference.
有没有办法可以将节点的内部文本保持在其当前的“无害”状态,并且仍然将其包装在也保存在 CDATA 中的节点中?可能可以更好地表达,但这些词没有浮现在脑海中。