【问题标题】:Re-escape characters in an XML file在 XML 文件中重新转义字符
【发布时间】:2019-07-27 18:12:57
【问题描述】:

考虑以下 XML 结构(在这种情况下,它是一个 RSS 提要)

<feed xmlns="http://www.w3.org/2005/Atom">
<link href="http://example.com/atom/" rel="self" type="application/rss+xml"/>
<link rel="alternate" href="http://example.com/" type="text/html"/>
<title type="text">Example RSS feed</title>
<updated>2019-07-27T13:59:14-04:00</updated>
<subtitle>Example</subtitle>
<icon>http://example.com/favicon-32x32.png</icon>
<logo>http://example.com/logo.png</logo>
<rights>© 2019 Example</rights>
<author>
<name>Keanu Reeves</name>
<email>me@example.com</email>
<uri>http://example.com</uri>
</author>
<id>http://example.com/</id>
<entry>
<title>Example post</title>
<id>http://example.com/post/example</id>
<link rel="alternate" href="http://example.com/post/example"/>
<summary type="html">
Description of post. (Preview thing)
</summary>
<updated>2019-07-27T13:59:14-04:00</updated>
<author>
<name>Keanu Reeves</name>
</author>
</entry>
</feed>

如果保存为 .atom 文件,则可以完美运行。

尽管如此,我想在我的帖子summary 中包含以下内容:

Example text, blah blah blah. <a href="/post/example">Read more...</a>
The above links get interpreted as litteral HTML when escaped correctly using the function under this code snippet. Good!
Now, heres litteral "<" and ">" characters.... <><><<<>>

我想包含的最后一行显然会使 .atom 文件无效。因此,我使用以下 PHP 函数将最后一行编码为符合 XML 标准:

echo htmlentities("Now, heres litteral \"<\" and \">\" characters.... <><><<<>>",ENT_XML1);

输出如下文本:

Now, heres litteral "&lt;" and "&gt;" characters.... &lt;&gt;&lt;&gt;&lt;&lt;&lt;&gt;&gt;

但现在,我所有的提要阅读器(Chrome 的 Slick RSS 和 android 的 FeedR)都将上述内容解释为文字 HTML!

那么我怎样才能重新逃脱那些呢?

干杯:)

【问题讨论】:

    标签: php html xml rss atom-feed


    【解决方案1】:

    因为在解析 XML 文档时,该字段的内容仍然包含文字 &lt;&gt; [以及可能的其他] 元字符。

    // the literal string you want to encode.
    $string1 = "Now, heres litteral \"<\" and \">\" characters.... <><><<<>>";
    
    // oops but I want to make sure I don't accidentally pass in HTML to RSS readers that might
    // accidentally try to render it.
    $string2 = htmlentities($string1);
    
    // oh also I am writing XML directly instead of using a proper library to generate the document.
    // I know that this is a really bad idea, but I'm sure I have my reasons.
    // anywho, I should escape this text to be kludged directly into an XML doc.
    $string3 = htmlentities($string2, ENT_XML1);
    
    var_dump($string1, $string2, $string3);
    

    输出:

    string(56) "Now, heres litteral "<" and ">" characters.... <><><<<>>"
    string(109) "Now, heres litteral &quot;&lt;&quot; and &quot;&gt;&quot; characters.... &lt;&gt;&lt;&gt;&lt;&lt;&lt;&gt;&gt;"
    string(169) "Now, heres litteral &amp;quot;&amp;lt;&amp;quot; and &amp;quot;&amp;gt;&amp;quot; characters.... &amp;lt;&amp;gt;&amp;lt;&amp;gt;&amp;lt;&amp;lt;&amp;lt;&amp;gt;&amp;gt;"
    

    $string2 如果您将数据输入到 XMLDocument、DomDocument 或类似对象之类的东西中,则应该根据需要进行编码,但是因为看起来您正在以艰难的方式做事一直到$string3

    【讨论】:

    • 感谢您的帮助! Tho,为什么在没有任何库的情况下制作自己的 RSS 提要是个坏主意?这是一个更大的项目(博客),我从头开始编码。我为什么要在里面塞一个库?
    • @keanu_reeves 因为这不是您要花时间重新解决的唯一问题,下一个可能会与此冲突一次,依此类推,直到您的代码库一个清醒的噩梦,乞求从头开始重写所有这些问题。现有的库通常是已经完成的多次迭代的结果,并且具有许多贡献者/维护者、良好的文档和其他开发人员在第一次处理您的项目之前已经知道如何使用它的额外好处。
    【解决方案2】:

    您定义了summary 中的片段是一个HTML 片段。

    <summary type="html">
    Description of post. (Preview thing)
    </summary>
    

    Atom 支持type 属性来定义如何处理内容。它甚至可以像视频一样编码二进制内容。

    html 类型读取节点的文本内容并将其呈现为 HTML 片段。 text 读取文本内容并将其作为纯文本输出。 xhtml 渲染后代节点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2011-04-26
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多