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"><collection></choice>
<choice value="V2"><lasso/> </choice>
<choice value="V3"><fieldset></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:
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 "<"" 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>
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 < and neither worked.
Either using this method or any other method, how do I prevent the < and > 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 "&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 <... or even &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"><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/>
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"><collection></choice>
<choice value="V2"><lasso/> </choice>
<choice value="V3"><fieldset></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"><collection></input>
<br/>
<input type="radio" value="V2"><lasso/> </input>
<br/>
<input type="radio" value="V3"><fieldset></input>
<br/>
<input type="radio" value="V4">No such tag exists</input>
<br/>
</multiple_choice>
rendered as:
链接地址: http://www.djcxy.com/p/87628.html下一篇: 在XSLT 1.0中转换时编码