尝试使用xslt将xml中的特殊字符转换为其编码形式。
示例:
& to &
" to "
< to <
> to >
等等。 我正在使用的代码如下所示
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates select="//search-results/items" />
</xsl:template>
<xsl:template match="items">
<textarea>
<xsl:apply-templates select="file-item" />
</textarea>
</xsl:template>
<xsl:template match="file-item">
<xsl:apply-templates select="." mode="details"/>
</xsl:template>
<xsl:template match="*" mode="details">
<file-item>
<id><xsl:value-of select = "@id"/></id>
<xsl:copy-of select = "name"/>
<xsl:copy-of select = "creation-date" />
<xsl:copy-of select = "modification-date"/>
<xsl:copy-of select = "file-info"/>
<xsl:copy-of select = "creator"/>
<xsl:copy-of select = "last-modifier"/>
</file-item>
</xsl:template>
</xsl:stylesheet>
XML结构
<id>5060554</id>
<name>This is a File && and it is a "Test File" </name>
<creation-date timestamp="1487516375360">19.02.2017 14:59</creation-date>
<modification-date timestamp="1488128705695">26.02.2017 17:05</modification-date>
<file-info>
<name>Background-Wallpaper & Nature.jpg</name>
<creator user-id="2196">
<last-modifier user-id="2120">
输出也应该包含xml节点,这就是我在textarea而不是xsl:value-of中使用xsl:copy的原因。 Becausse xsl:value-of select =&#34; name&#34;只输出这是一个文件&amp;&amp;它是一个&#34;测试文件&#34; ,而xsl:copy-of将产生这是一个文件&amp;&amp;它是一个&#34;测试文件&#34;
使用XSLT版本1.o
正在寻找的所需输出是
的 This is a File & & and it is a "Test File"
答案 0 :(得分:1)
You say that the input XML contains
<name>This is a File && and it is a "Test File" </name>
That's a contradiction. If it contains that string, then it isn't XML. Ampersands in XML are always escaped. And if the input isn't XML, then you can't use XSLT to process it.
You say "we are converting the data to xml and then to html (UI view of the website) using XSLT." It seems you are converting the data to XML incorrectly, and you need to fix that.