首页 文章

在XSLT 1.0中转换时,带有文本标记的XML过早地未编码

提问于
浏览
0

我正在努力将存储在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">&lt;collection&gt;</choice>
    <choice value="V2">&lt;lasso/&gt;   </choice>
    <choice value="V3">&lt;fieldset&gt;</choice>
    <choice value="V4">No such tag exists</choice>
  </multiple_choice>

...当它被渲染时它已被解码,并且问题显示在屏幕上,a,b和c为空白,字段集围绕d:

Result of XSL

我在堆栈上找到了这个,修改了最小的一点,并使用它:

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<!-- Replace < encoding "&lt;"" with double encoded string "&amp;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='"&lt;"' />
            <xsl:with-param name="to" select='"&amp;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">&lt;collection&gt;</choice>
<input type="radio" value="V2"/><choice value="V2">&lt;lasso&gt;</choice>
<input type="radio" value="V3"/><choice value="V3">&lt;fieldset&gt;</choice>
<input type="radio" value="V4"/><choice value="V4">No such tag exists</choice>

1 回答

  • 0

    你的问题相当令人困惑 . 如果这是您的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">&lt;collection&gt;</choice>
        <choice value="V2">&lt;lasso/&gt;   </choice>
        <choice value="V3">&lt;fieldset&gt;</choice>
        <choice value="V4">No such tag exists</choice>
    </multiple_choice>
    

    为什么有必要更换任何东西? Yoi没有发布预期的结果HTML代码,但作为示例,应用此样式表:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="choice">
        <input type="radio" value="{@value}">
            <xsl:value-of select="."/>
        </input>
    </xsl:template> </xsl:stylesheet>

    会产生以下 result

    <multiple_choice>
       <question>
          <p style="font-weight: bold">What is the HTML tag used to create a collection of input fields?</p>
       </question>
       <input type="radio" value="V1">&lt;collection&gt;</input>
       
    <input type="radio" value="V2">&lt;lasso/&gt; </input>
    <input type="radio" value="V3">&lt;fieldset&gt;</input>
    <input type="radio" value="V4">No such tag exists</input>
    </multiple_choice>

    呈现为:

    enter image description here

相关问题