XML 文本标签在 XSLT 1.0 中转换时过早取消编码
XML with text tags getting prematurely un-encoded when transformed in XSLT 1.0
我正在努力将存储在 XML 中的多项选择测验转换为 HTML...答案包括 html 标签:
What is the HTML tag used to create a collection of input fields
a) <collection>
b) <lasso>
c) <fieldset>
d) No such tag exists
问题在于,虽然文本是在 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 周围有一个字段集:
我在 Stack 上找到了这个,对它进行了最细微的修改,然后使用了它:
<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",它会非常有效,但如果我想更改 < ,我根本没有任何改变。
我用 < 和 < 试过了都没有用。
无论是使用此方法还是任何其他方法,我该如何防止 < >从渲染之前解码?
更新 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
灵光一现...我将 "from" 字符串更改为“<”它能够找到它...这很棒,如果我愿意,我可以轻松地将其更改为“[”...
但是我无法将其更改为 <... 甚至 &amp;amp;lt; ...无论出于何种原因,xsl 将遍历任意数量的编码和 return 未编码小于
更新 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><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/>
我希望通过浏览器接收的来源是:
<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/>
你的问题比较混乱。如果这是您的 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>
为什么需要更换任何东西? Yoi 没有 post 预期的结果 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><br/>
</xsl:template>
</xsl:stylesheet>
会产生以下结果:
<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>
呈现为:
我正在努力将存储在 XML 中的多项选择测验转换为 HTML...答案包括 html 标签:
What is the HTML tag used to create a collection of input fields
a) <collection>
b) <lasso>
c) <fieldset>
d) No such tag exists
问题在于,虽然文本是在 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 周围有一个字段集:
我在 Stack 上找到了这个,对它进行了最细微的修改,然后使用了它:
<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",它会非常有效,但如果我想更改 < ,我根本没有任何改变。
我用 < 和 < 试过了都没有用。
无论是使用此方法还是任何其他方法,我该如何防止 < >从渲染之前解码?
更新 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
灵光一现...我将 "from" 字符串更改为“<”它能够找到它...这很棒,如果我愿意,我可以轻松地将其更改为“[”...
但是我无法将其更改为 <... 甚至 &amp;amp;lt; ...无论出于何种原因,xsl 将遍历任意数量的编码和 return 未编码小于
更新 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><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/>
我希望通过浏览器接收的来源是:
<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/>
你的问题比较混乱。如果这是您的 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>
为什么需要更换任何东西? Yoi 没有 post 预期的结果 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><br/>
</xsl:template>
</xsl:stylesheet>
会产生以下结果:
<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>
呈现为: