我正在努力将存储在XML中的多项选择测验转换为HTML ...答案包括html标签:
用于创建输入字段集合的HTML标记是什么a)<collection> b)<lasso> c)<fieldset> d)不存在此类标记
问题是文本是用XML编码的;
<multiple_choice>
<question>
<p style="font-weight: bold">What is the HTML tag used to create a collection of input fields?</p>
</question>
<choice value="V1"><collection></choice>
<choice value="V2"><lasso/> </choice>
<choice value="V3"><fieldset></choice>
<choice value="V4">No such tag exists</choice>
</multiple_choice>
...当它被渲染时它已被解码,并且问题显示在屏幕上,a,b和c为空白,字段集围绕d:
我在堆栈上找到了这个,修改了最小的一点,并使用它:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Replace < encoding "<"" with double encoded string "&lt;" -->
<xsl:template match="choice/text()">
<xsl:variable name="newtext">
<xsl:call-template name="string-replace">
<xsl:with-param name="string" select='.' />
<xsl:with-param name="from" select='"<"' />
<xsl:with-param name="to" select='"&lt;"' />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$newtext"/>
</xsl:template>
如果我想将“fieldset”更改为“monkey”但是如果我想要改变<,那么哪个可以工作,我根本没有变化 .
我用<和with&lt;尝试了它并没有奏效 .
使用此方法或任何其他方法,我如何防止&lt;和&gt;从渲染前的解码?
更新1
字符串替换非常传统,但我应该考虑包含它:
<xsl:template name="string-replace" >
<xsl:param name="string" />
<xsl:param name="from" />
<xsl:param name="to" />
<xsl:choose>
<xsl:when test="contains($string,$from)"> <xsl:value-of select="substring-before($string,$from)"/><xsl:value-of select="$to"/> <xsl:call-template name="string-replace"> <xsl:with-param name="string" select="substring-after($string,$from)"/> <xsl:with-param name="from" select="$from"/> <xsl:with-param name="to" select="$to"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise> <xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
更新2
轻微的尤里卡时刻......我将“从”字符串更改为“&amp; lt;”并且它能够找到它...这很棒,如果我想要的话,我可以将它改为“[”
但是我无法将其更改为&lt; ...或者甚至&amp; amp; amp; amp; lt; ...无论出于何种原因,xsl将遍历任意数量的编码并返回未编码的小于
更新3
我在浏览器上收到的实际来源是:
<p style="font-weight: bold">What is the HTML tag used to create a collection of input fields?</p>
<input type="radio" value="V1"/><choice value="V1"><collection></choice>
<input type="radio" value="V2"/><choice value="V2"><lasso></choice>
<input type="radio" value="V3"/><choice value="V3"><fieldset></choice>
<input type="radio" value="V4"/><choice value="V4">No such tag exists</choice>
我希望在浏览器中收到的来源是:
<p style="font-weight: bold">What is the HTML tag used to create a collection of input fields?</p>
<input type="radio" value="V1"/><choice value="V1"><collection></choice>
<input type="radio" value="V2"/><choice value="V2"><lasso></choice>
<input type="radio" value="V3"/><choice value="V3"><fieldset></choice>
<input type="radio" value="V4"/><choice value="V4">No such tag exists</choice>
1 回答
你的问题相当令人困惑 . 如果这是您的XML输入:
为什么有必要更换任何东西? Yoi没有发布预期的结果HTML代码,但作为示例,应用此样式表:
XSLT 1.0
会产生以下 result :
呈现为: