使用CDATA进行xml,xsl转换 [英] xml, xsl transformation with CDATA

查看:110
本文介绍了使用CDATA进行xml,xsl转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xsl xml转换的新手.现在,我有一个包含以下信息的xml文件:

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
   &quot;<a href="http://www.last.fm/music/Majesty"
   class="bbcode_artist">Majesty</a>&quot; 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文件包含以下内容:

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>

我现在想要做的是从<summary><[CDATA[]]></summary>中提取标记结构的数据,并像下面的示例一样在浏览器中显示它:

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>

现在,当我打开xml页面时,它会显示所有CDATA内容,即使带有那些html标记...我想让这些标记以html形式正确地完成其工作.

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

推荐答案

CDATA只是文本节点的一部分,而其中的标记似乎是一维文本(标记被严重破坏),并且在不调用扩展功能的情况下无法完成此操作(在XSLT 1.0和XSLT 2.0中).

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>

在XSLT 3.0中,可能会有一个新的标准函数 更新:

这里是一个完整的代码示例,假设您在.NET中使用XslCompiledTransform :

Here is a complete code example, assuming you are using XslCompiledTransform in .NET:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="summary/text()">
  <xsl:copy-of select="my:parse(.)/*/*"/>
 </xsl:template>

 <msxsl:script language="c#" implements-prefix="my">
  public XmlDocument parse(string text)
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("&lt;t>"+text+"&lt;/t>");

    return doc;
  }
 </msxsl:script>
</xsl:stylesheet>

将此转换应用于提供的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 &quot;<a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a>&quot; 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>

产生了所需的正确结果(CDATA被重新构造的标记代替):

<bio>
  <published>Tue, 7 Oct 2008 14:47:26 +0000</published>
  <summary>
    <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>
  </summary>
</bio>

这篇关于使用CDATA进行xml,xsl转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆