【问题标题】:How to change a certain part of XML file using XSL.?如何使用 XSL 更改 XML 文件的某个部分。?
【发布时间】:2013-06-13 16:29:21
【问题描述】:

有一个 RSS 提要 xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
    <channel>
        <item>
            <title>news title 1</title>
            <link>http://URL</link>
            <description>
                <div><b>Article_Title:</b> AAAAA</div>
                <div><b>Article_Summary:</b> AAAAAA</div>
                <div><b>Article_Date:</b> 05/08/2013</div>
            </description>
            <author>QWERT</author>
            <pubDate>Mon, 27 May 2013 16:13:50 GMT</pubDate>
            <guid isPermaLink="true">http://URL</guid>
        </item>
        <item>
            <title>news title 2</title>
            <link>http://URL</link>
            <description>
                <div><b>Article_Title:</b>BBB</div>
                <div><b>Article_Summary:</b>BBB</div>
                <div><b>Article_Date:</b> 05/10/2013</div>
            </description>
            <author>ASDF</author>
            <pubDate>Tue, 28 May 2013 09:50:51 GMT</pubDate>
            <guid isPermaLink="true">http://URL</guid>
        </item>
    </channel>
</rss>

此 RSS Feed XML 文件驻留在服务器上。我正在获取此 XML,并希望使用 XSL 对 &lt;description&gt; 标记进行一些更改。

所有这些都应该发生在客户端。所以我想在客户端对原始 RSS 提要文件进行更改。

是否可以使用 XSL 更改原始文件。

提前致谢。

【问题讨论】:

标签: jquery xml xslt xml-parsing rss


【解决方案1】:

使用 importStylesheet 获取 .xsl 文件以及 XSLTProcessor JavaScript API 的 transformToFragment 方法:

function transform()
  {
  var nameOfPage = var value=RegExp("nameOfPage[^&]+").exec(window.location.search)[0].replace(/[^=]+./,"");

  with (new XSLTProcessor)
     {
     setParameter(null, "nameOfPage", nameOfPage);
     importStylesheet("foo.xsl");
     transform.result = transformToFragment(this, document);
     transform.root = transform.result.xml;
     document.getElementById(appendTo).appendChild(transform.root);
     }
  }

其中 foo.xsl 是这样的:

<?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" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<xsl:template match='//channel'>
 <page>
  <content>
   <module>
    <header layout="simple">
     <layout-items>
      <block class="title">YDN Widget</block>
     </layout-items>
    </header>
   </module>
   <xsl:apply-templates select="item" />
  </content>
 </page>
</xsl:template>

</xsl:stylesheet>

应用 XSLT 并将转换后的结果附加到您的文档中。

参考文献

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多