使用 CDATA 进行 xml、xsl 转换

发布于 2024-12-18 05:18:32 字数 1846 浏览 2 评论 0原文

我是 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
   &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 文件包含以下信息:

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

我现在要做的是从

<[ CDATA[]]> 并在浏览器中显示它,如下例所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-12-25 05:18:32

CDATA 只是文本节点(的一部分),其内部的标记看起来是一维文本(严重破坏的标记),如果不这样做,这是无法实现的(在 XSLT 1.0 和 XSLT 2.0 中)调用扩展函数。

在 XSLT 3.0 中,可能有新的标准函数 parse-xml() 正是这样做的

更新

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

<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("<t>"+text+"</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 "<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>

生成了所需的正确结果(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>

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:

<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("<t>"+text+"</t>");

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

When this transformation is applied on the provided XML document:

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

the wanted, correct result (the CDATA is replaced by the reconstituted markup) is produced:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文