encoded when transformed in XSLT 1.0

I'm working on transforming to HTML a multiple choice quiz stored in XML... the answers include html tags:

What is the HTML tag used to create a collection of input fields
a) <collection>
b) <lasso>
c) <fieldset>
d) No such tag exists

The problem is that while the text is encoded in the 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>

...by the time it gets rendered it has been unencoded, and the question displays on the screen with a, b, and c as blank, with a fieldset around d:

XSL的结果

I found this here on Stack, tinkered it the tiniest bit, and used it:

<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>

Which works SMASHINGLY if I want to change "fieldset" to "monkey" but if I want to change < , I get no change at all.

I tried it with < and with &lt; and neither worked.

Either using this method or any other method, how do I prevent the &lt; and &gt; from decoding before the render?

Update 1

String replace is pretty traditional, but I should have thought to include it:

<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> 

Update 2

Minor eureka moment... I changed the "from" string to "&amp;lt;" and it was able to find it... which is great, I can change it to "[" easily if I wanted...

However I can't change it to &lt;... or even &amp;amp;amp;amp;lt; ... for whatever reason, the xsl will traverse any number of encodes and return an unencoded less than

Update 3

The actual source I'm recieving at the browser is:

<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><br/>
<input type="radio" value="V2"/><choice value="V2"><lasso></choice><br/>
<input type="radio" value="V3"/><choice value="V3"><fieldset></choice><br/>
<input type="radio" value="V4"/><choice value="V4">No such tag exists</choice><br/>

The source I desire to receiving at the browser is:

<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><br/>
<input type="radio" value="V2"/><choice value="V2">&lt;lasso&gt;</choice><br/>
<input type="radio" value="V3"/><choice value="V3">&lt;fieldset&gt;</choice><br/>
<input type="radio" value="V4"/><choice value="V4">No such tag exists</choice><br/>

Your question is rather confusing. If this is your XML input:

<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>

why is it necessary to replace anything? Yoi did not post the expected resulting HTML code, but as an example, applying this stylesheet:

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><br/>
</xsl:template>

</xsl:stylesheet>

would produce the following 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>
   <br/>
   <input type="radio" value="V2">&lt;lasso/&gt;   </input>
   <br/>
   <input type="radio" value="V3">&lt;fieldset&gt;</input>
   <br/>
   <input type="radio" value="V4">No such tag exists</input>
   <br/>
</multiple_choice>

rendered as:

在这里输入图像描述

链接地址: http://www.djcxy.com/p/87628.html

上一篇: QTextEdit背景颜色也会改变滚动条的颜色

下一篇: 在XSLT 1.0中转换时编码