【问题标题】:Use HTML tags in XML file and display/recognize them later as HTML在 XML 文件中使用 HTML 标记并稍后将它们显示/识别为 HTML
【发布时间】:2018-10-31 12:43:48
【问题描述】:

我正在尝试在 xml 文件中使用 HTML 标签,该文件稍后在视图中用于显示内容。我的问题,HTML 标记作为纯字符串传递,尽管我适当地转义了所有必要的字符。

<?xml version='1.0'?>  
    <selfassessment>
        <topic>
            <description>Die &lt;strong&gt; Zweckbestimmung &lt;/strong&gt; ist ein eleme
                Konsequenzen.</description>
            <shortdesc>Zweckbestimmung</shortdesc>
            <question>
                <questiontext>Ihre fest:</questiontext>
                <answer correct="false">Sie habenmung.</answer>
                <answer correct="true"> erreichen sollen.</answer>
                <answer correct="true">Das phyiert.</answer>
                <answer correct="true">Die Chlektuellen eiten.</answer>
                <answer correct="true">Die Beler &quot;Workload&quot;.</answer>
                <answer correct="true">Date.</answer>
                <comment>All  &lt;br/&gt;&lt;a href=&quot;/someLink&quot; class=&quot;button&quot;&gt;Erfahren Sie hier mehr&lt;/a&gt;</comment>
            </question>
        </topic>
     </selfassessment>

显示的时候不实现html标签,直接粘贴到纯字符串中。

我已经看到这里提出的所有相关问题,我尝试使用 CDATA,但没有成功。我在这里缺少什么吗?它应该像这样工作,不是吗?

提前致谢。

编辑:(涉及php代码)

获取xml文件:

function getXmlFile(){
        $xml = null;

        $xmlFile = '/var/www/html/typo3conf/ext/extKeyHere/Resources/Private/XML/data.xml';

        if (file_exists($xmlFile)) {
            $xml = simplexml_load_file($xmlFile);
        } else {
         exit("Datei $xmlFile kann nicht geöffnet werden."); 
         //echo "Datei $xmlFile kann nicht geöffnet werden."; 
        } 
        return $xml;
    }

后来我像这样使用它:

    function __construct($xmldata) {
        $this->xml = $xmldata;
        $this->calculateResults();
    }

    private function calculateResults() {

        $page = 0;

        //iterating over xml file
        foreach ($this->xml->topic as $thema) {
            $this->topics[$page] = $thema->description;
        and so on.....

        //passing it to the view
        $this->view->assignMultiple(array( 
        'allQuestions' => $allQuestions,
        'questions' => $questions,
        ....

视图内的代码:

<form method='post' action="{url}">
<h3>{allQuestions.themaDescr}</h3>
<f:for each="{allQuestions.thema.question}" as="question" iteration="i">
   <div class='question'><p><strong>Frage {i.cycle}</strong>: 
    {question.questiontext}</p>
    <f:for each="{question.answer}" as="answer" iteration="ii">
      <div class='answer'><input type='checkbox' name='A{page} 
      {i.cycle}{ii.cycle}'/>
        <div class='subanswer' for='A{page}{i.cycle}{ii.cycle}'> 
         {answer}
        </div>
      </div>
   </f:for>
  </div>
</f:for>
    <input type='hidden' name='page' value='{allQuestions.page}'/>
    <f:if condition="{allQuestions.nextThema}">
        <f:then>
            <button type='submit'>Ergebnisse anzeigen</button>
        </f:then>
        <f:else>`
            <button type='submit'>Nächste Seite</button>
        </f:else>
    </f:if>
</form>

没有错误,除了我提到的 html 标签显示为纯文本外,一切正常。 顺便说一句,这是 Typo3,但我认为这并不重要,除了我如何将数据传递给视图并显示它。

【问题讨论】:

  • 我在该代码中没有看到任何CDATA
  • 您可以从 URL 获取示例:stackoverflow.com/questions/4412395/…
  • @kerbholz 也许我不够清楚。我也尝试过使用 CDATA,但它没有用。但我认为在转义 html 标记字符后它应该仍然可以工作。
  • @Vish 我已经阅读了这个问题,但它对我不起作用。对于 CDATA 部分,HTML 标记也无法识别,最终也以纯字符串形式结束,或者被完全忽略
  • 如何从 XML 中提取 HTML 并显示它?这是关键部分。

标签: php html xml


【解决方案1】:

目前还不清楚你的问题是什么,你的目标是什么。

让我尝试做一些尝试来帮助你。

第一件事 - 你有数据:

<description>Die &lt;strong&gt; Zweckbestimmung &lt;/strong&gt; ist ein eleme Konsequenzen.</description>
<comment>All  &lt;br/&gt;&lt;a href=&quot;/someLink&quot; class=&quot;button&quot;&gt;Erfahren Sie hier mehr&lt;/a&gt;</comment>

该数据部分具有 html 编码字符。关键是永远不要将数据与真正的 html 或 xml 混合。

所以当你这样做时:

$this->topics[$page] = $thema->description;

您的值仍然包含 html 编码的字符:

$this->topics[$page] === `Die &lt;strong&gt; Zweckbestimmung &lt;/strong&gt; ist ein eleme Konsequenzen.`

从您的角度来看,这可能有点错误。但对我来说,目前我还没有看到任何问题。

然后你做:

 <div class='question'><p><strong>Frage {i.cycle}</strong>: 
{question.questiontext}</p>

让我们简化并只关注这部分:

 <p>{question.questiontext}</p>

在您的数据 xml 中不清楚的一件事是没有名称为 questiontext 的字段。所以让我们假设是topic-&gt;question-&gt;comment。

你的价值意味着:

&lt;p&gt;All  &amp;lt;br/&amp;gt;&amp;lt;a href=&amp;quot;/someLink&amp;quot; class=&amp;quot;button&amp;quot;&amp;gt;Erfahren Sie hier mehr&amp;lt;/a&amp;gt;&lt;/p&gt;

而你可能不乐意得到你期望得到的那样:

&lt;p&gt;All &lt;br/&gt;&lt;a href="/someLink" class="button"&gt;Erfahren Sie hier mehr&lt;/a&gt;&lt;/p&gt;

危险如果您使用 htmlspecialchars_decode 函数将 html 实体解码回未编码的字符,则可以这样做:

// or whatever part you have to get comment
$this->topics[$page]['topic']['question']['comment'] = htmlspecialchars_decode($thema->question->comment);

这应该按您期望的方式工作。但这是非常危险的。当您允许数据​​包含 html 未编码部分时,您对xss 开放。

【讨论】:

  • 感谢您的帮助,我想通了是因为您。但我是通过使用 viewhelpers f:format.htmlentitiesDecode 方法做到的。现在 HTML 标签被识别为 HTML 而不是纯文本。
猜你喜欢
  • 2020-11-05
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
相关资源
最近更新 更多