使用 CDATA 进行 xml、xsl 转换
我是 xsl xml 转换的新手。现在,我有一个 xml 文件,其中包含以下信息:
<bio>
<published>Tue, 7 Oct 2008 14:47:26 +0000</published>
<summary><![CDATA[
Dream Theater is an American <a
href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag"
rel="tag">progressive metal</a> band formed in 1985 under the name
"<a href="http://www.last.fm/music/Majesty"
class="bbcode_artist">Majesty</a>" by <a
href="http://www.last.fm/music/John+Myung"
class="bbcode_artist">John Myung</a>,
<a href="http://www.last.fm/music/John+Petrucci"
class="bbcode_artist">John Petrucci</a>
]]>
</summary>
</bio>
我的 xsl 文件包含以下信息:
<h3><xsl:value-of select="lfm/artist/bio/published"/></h3>
<p>
<xsl:value-of select="lfm/artist/bio/summary" disable-output-escaping="yes"/>
</p>
<html>
<body>
<xsl:value-of select="lfm/artist/bio/content"/>
</body>
</html>
我现在要做的是从
并在浏览器中显示它,如下例所示:
<a href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" rel="tag">progressive metal</a>
<a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a>
<a href="http://www.last.fm/music/John+Myung" class="bbcode_artist">John Myung</a>
<a href="http://www.last.fm/music/John+Petrucci" class="bbcode_artist">John Petrucci</a>
现在,当我打开 xml 页面时,它会显示所有 CDATA
内容,甚至与那些 html 标签...我想让这些标签以 html 形式正确完成它们的工作。
抱歉,我的描述很糟糕……希望你们能明白我的意思……
I am new to xsl xml transformation. For now, I have an xml file that contains the following information:
<bio>
<published>Tue, 7 Oct 2008 14:47:26 +0000</published>
<summary><![CDATA[
Dream Theater is an American <a
href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag"
rel="tag">progressive metal</a> band formed in 1985 under the name
"<a href="http://www.last.fm/music/Majesty"
class="bbcode_artist">Majesty</a>" by <a
href="http://www.last.fm/music/John+Myung"
class="bbcode_artist">John Myung</a>,
<a href="http://www.last.fm/music/John+Petrucci"
class="bbcode_artist">John Petrucci</a>
]]>
</summary>
</bio>
And my xsl file contains this:
<h3><xsl:value-of select="lfm/artist/bio/published"/></h3>
<p>
<xsl:value-of select="lfm/artist/bio/summary" disable-output-escaping="yes"/>
</p>
<html>
<body>
<xsl:value-of select="lfm/artist/bio/content"/>
</body>
</html>
What I'm trying to do now is to extract the tag-structured data out of the <summary><[CDATA[]]></summary>
and show it in the browser as in this example:
<a href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" rel="tag">progressive metal</a>
<a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a>
<a href="http://www.last.fm/music/John+Myung" class="bbcode_artist">John Myung</a>
<a href="http://www.last.fm/music/John+Petrucci" class="bbcode_artist">John Petrucci</a>
For now when I open the xml page, it shows all the CDATA
content, even with those html tags... I want to get those tags to do their job properly in html form.
sorry for the terrible description..hope you guys can get what i mean here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CDATA 只是文本节点(的一部分),其内部的标记看起来是一维文本(严重破坏的标记),如果不这样做,这是无法实现的(在 XSLT 1.0 和 XSLT 2.0 中)调用扩展函数。
在 XSLT 3.0 中,可能有新的标准函数
parse-xml()
正是这样做的。更新:
这里是一个完整的代码示例,假设您在 .NET 中使用
XslCompiledTransform
:当此转换应用于提供的XML 文档:
生成了所需的正确结果(CDATA 被重构的标记替换):
The CDATA is just (a part of) a text node and what seems like markup inside it is one-dimensional text (badly destroyed markup) and this cannot be accomplished (in XSLT 1.0 and XSLT 2.0) without calling an extension function.
<p><xsl:copy-of select="my:parse(lfm/artist/bio/summary)"></p>
In XSLT 3.0 there may be a new standard function
parse-xml()
that does exactly this.Update:
Here is a complete code example, assuming you are using
XslCompiledTransform
in .NET:When this transformation is applied on the provided XML document:
the wanted, correct result (the CDATA is replaced by the reconstituted markup) is produced: