简体   繁体   English

Mozilla不会解释我的xml中包含的HTML

[英]Mozilla does not interpret the HTML contained in my xml

I don't speak English very well because I'm french so sorry if I make mistakes. 我英语说得不太好,因为我是法语,所以如果我犯错了,抱歉。 I have this code : 我有这个代码:

<xsl:when test="$PARAM=1">
        <xsl:if test="LabelType=0 or LabelType=1 or LabelType=2 or LabelType=3 or LabelType=4 or LabelType=5 or LabelType=9 or LabelType=11 or LabelType=99 or LabelType=98 or LabelType=15 or LabelType=14" >
          <div class="sst">
            <table style="font-size: 12px">
              <tr>
                <td>
                  <xsl:choose>
                    <xsl:when test="LabelType=1">
                      <img src="Images/Atcd/stethoscope-icon.png" alt="Consultation" />
                      Consultation
                    </xsl:when>
                    <xsl:when test="LabelType=2">
                      <img src="Images/Atcd/Pen-icon.png" alt="Certificat" />
                      Certificat
                    </xsl:when>
                    <xsl:when test="LabelType=3">
                      <img src="Images/Atcd/Pen-3-icon.png" alt="Demande" />
                      Demande
                    </xsl:when>
                    <xsl:when test="LabelType=4">
                      <img src="Images/Atcd/pill-icon.png" alt="Prescription" />
                      Prescription
                    </xsl:when>
                    <xsl:when test="LabelType=5">
                      <img src="Images/options-icon2.png" alt="Formulaire" />
                      Formulaire
                    </xsl:when>
                    <xsl:when test="LabelType=8">
                      <img src="Images/Atcd/Bio-hazard-icon.png" alt="Vaccin" />
                      Vaccin
                    </xsl:when>
                    <xsl:when test="LabelType=9">
                      <img src="Images/Atcd/Coins-icon.png" alt="Reglement" />
                      Reglement
                    </xsl:when>
                    <xsl:when test="LabelType=10">
                      <img src="Images/Atcd/Paper-Clip-icon.png" alt="Binary" />
                      Binary
                    </xsl:when>
                    <xsl:when test="LabelType=11">
                      <img src="Images/Atcd/BPink.png" alt="Grossesse" />
                      Grossesse
                    </xsl:when>
                    <xsl:when test="LabelType=15">
                      <img src="Images/Atcd/stethoscope-icon.png" alt="Planche" />
                      Planche
                    </xsl:when>
                    <xsl:when test="LabelType=14">
                      <img src="Images/Atcd/stethoscope-icon.png" alt="Audio" />
                      Audio
                    </xsl:when>
                    <xsl:when test="LabelType=99">
                      <img src="Images/Atcd/twitter-bulb-icon.png" alt="Conclusion" />
                      Conclusion
                    </xsl:when>
                    <xsl:when test="LabelType=98">
                      <img src="Images/Atcd/You-make-me-hurt-icon.png" alt="A.T." />
                      A.T.
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:if test="LabelType != 0">
                        <img src="Images/Atcd/Medical-invoice-information-icon.png" alt="Inconnu" />
                        Inconnu <xsl:value-of select="LabelType"/>
                      </xsl:if>
                    </xsl:otherwise>
                  </xsl:choose>
                </td>
              </tr>
            </table>
          </div>
          <xsl:if test="Texte1">
            <xsl:if test="Title1">
              <div class="sst2">
                <xsl:value-of disable-output-escaping="yes"  select="Title1"/>
              </div>
            </xsl:if>
            <div class="stx">
              <xsl:value-of disable-output-escaping="yes"  select="Texte1"/>
            </div>
          </xsl:if>
          <xsl:if test="LabelType!=5 and LabelType!=15 and LabelType!=14">
            <xsl:if test="Texte2">
              <xsl:if test="Title2">
                <div class="sst2">
                  <xsl:value-of disable-output-escaping="yes"  select="Title2"/>
                </div>
              </xsl:if>
              <div class="stx">
                <xsl:value-of disable-output-escaping="yes"  select="Texte2"/>
              </div>
            </xsl:if>
            </xsl:if>
          <xsl:if test="LabelType!=15 and LabelType!=14">
            <xsl:if test="Texte3">
              <xsl:if test="Title3">
                <div class="sst2">
                  <xsl:value-of disable-output-escaping="yes"  select="Title3"/>
                </div>
              </xsl:if>
              <div class="stx">
                <xsl:value-of disable-output-escaping="yes"  select="Texte3"/>
              </div>
            </xsl:if>
            </xsl:if>
          <xsl:if test="LabelType!=15 and LabelType!=14">
            <xsl:if test="Texte4">
              <xsl:if test="Title4">
                <div class="sst2">
                  <xsl:value-of disable-output-escaping="yes"  select="Title4"/>
                </div>
              </xsl:if>
              <div class="stx">
                <xsl:value-of disable-output-escaping="yes"  select="Texte4"/>
              </div>
            </xsl:if>
            </xsl:if>
          <xsl:apply-templates select="Suivis">
            <xsl:with-param name="viewtype" select="$viewtype" />
          </xsl:apply-templates>
        </xsl:if>
      </xsl:when>

This genarate a lot of "div" and it works very good on chrome and IE but in Mozilla disable-output-escaping doesn't work and I have some blocks of HTML code not interpreted. 这会产生很多“ div”,并​​且在chrome和IE上效果很好,但是在Mozilla中,disable-output-escapeing不起作用,并且我有一些HTML代码块无法解释。 I tryied a lot of things but any solutions work. 我尝试了很多事情,但是任何解决方案都可以。 In Javascript I can take the contents of a div and put it back in an other div with InnerHTML but it works only for one div not for all div. 在Javascript中,我可以使用div的内容,然后使用InnerHTML将其放回另一个div中,但是它仅适用于一个div,不适用于所有div。 I think it's because it's a loop when and I can't call multiple div by the same id. 我认为这是因为这是一个循环,而我无法通过相同的ID调用多个div。 So can you help me please ? 那你能帮我吗? I'm blocked since one week. 一个星期以来,我被封锁了。

XML : XML:

<Document>
<LabelType>4</LabelType>
<Texte1>&lt;div&gt;&lt;span style=&quot;font-family: inherit; font-size: inherit&quot;&gt;&lt;STRONG&gt;ZOLPIDEM ALMUS 10 mg Cpr pell séc Plq/14&lt;/STRONG&gt;&lt;/span&gt; &lt;span style=&quot;font-family: inherit; color:#0000ff; font-size: inherit;&quot;&gt;(2 boîtes)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: inherit; font-size: inherit&quot;&gt;&lt;div&gt;Prendre un comprimé au coucher, pendant un  mois &lt;/div&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;</Texte1>
</Document>

Output : 输出:

<div><span style="font-family: inherit; font-size: inherit"><STRONG>ZOLPIDEM ALMUS 10 mg Cpr pell séc Plq/14</STRONG></span> <span style="font-family: inherit; color:#0000ff; font-size: inherit;">(2 boîtes)</span></div><div style="font-family: inherit; font-size: inherit"><div>Prendre un comprimé au coucher, pendant un mois </div></div><div>&nbsp;</div>

What I want : 我想要的是 :

ZOLPIDEM ALMUS 10 mg Cpr pell séc Plq/14 (2 boîtes) Prendre un comprimé au coucher, pendant un mois ZOLPIDEM ALMUS 10毫克CprsécPlq / 14(2瓶)Prendre uncompriméau沙发床,垂饰

Relying on disable-output-escaping is usually the worst possible alternative. 通常依赖于disable-output-escaping是最糟糕的选择。 You should do so only when there are no others. 仅在没有其他人时才应这样做。 The XSLT spec actually says that: XSLT规范实际上表示:

An XSLT processor will only be able to disable output escaping if it controls how the result tree is output. XSLT处理器只有控制结果树的输出方式,才能够禁用输出转义。 This may not always be the case. 并非总是如此。 For example, the result tree may be used as the source tree for another XSLT transformation instead of being output.An XSLT processor is not required to support disabling output escaping. 例如,结果树可以用作另一个XSLT转换的源树而不是输出。不需要XSLT处理器来支持禁用输出转义。 [...] [...]

Since disabling output escaping may not work with all XSLT processors and can result in XML that is not well-formed, it should be used only when there is no alternative . 由于禁用输出转义可能不适用于所有XSLT处理器,并且可能导致XML格式不正确, 因此仅在没有其他选择时才应使用它

(XSLT 1.0 recommendation, section 16.4; emphasis added) (XSLT 1.0建议,第16.4节;增加了重点)

If you think about it, disabling output escaping for a task such as yours assumes a processing model in which the serialized output of the transformation is re-parsed. 如果考虑到这一点,则为诸如您这样的任务禁用输出转义将采用一种处理模型,在该模型中将重新解析转换的序列化输出。 Only in that way could text having the form of HTML be transformed into actual HTML elements in XSLT's data model. 只有这样,HTML形式的文本才能在XSLT的数据模型中转换为实际的HTML元素。 When you are performing the transformation inside a browser, however, it seems entirely plausible to me that the browser won't actually output the result at all, much less re-parse it, because the transformation result is already a document that it can, in principle, use as-is. 但是,当您在浏览器中执行转换时,在我看来,浏览器实际上根本不会输出结果,更不用说重新解析了,因为转换结果已经是它可以提供的文档,原则上按原样使用。

Ultimately, then, Firefox's reported behavior with the given input is fully permitted by the relevant specifications. 最终,相关规范完全允许Firefox报告具有给定输入的行为。 That other browsers behave differently is not a counter-indication (and the behavior you describe for them is also permitted). 其他浏览器的行为也不是反指示(也允许您为它们描述的行为)。 It seems unlikely that any approach depending on disable-output-escaping -- or more generally on the browser re-parsing the XSLT output -- will meet your needs. 似乎所有依赖于disable-output-escaping (或更普遍地说,取决于浏览器重新解析XSLT输出)都无法满足您的需求。

Essentially, what you would need to do is parse the string values of some of your input elements as HTML (or XHTML) to get a node-set or result-tree fragment to work with (assuming XSLT 1.0). 本质上,您需要做的是将某些输入元素的字符串值解析为HTML(或XHTML),以获得可以使用的节点集或结果树片段(假设XSLT 1.0)。 Questions along those lines have been asked here many times, and all that offer any solution other than disable-output-escaping have reached that conclusion. 这些问题一直在这里被问过很多次,所有提供除disable-output-escaping之外的解决方案的人都得出了这个结论。 Among those that offer specific variations are: 提供特定变体的有:

  • xslt convert xml string in xml elements xslt在xml元素中转换xml字符串
    • the second answer demonstrates building a mini XML parser in XSLT 第二个答案演示了如何在XSLT中构建一个小型XML解析器
  • Two phase XSLT transformation converting string to XML first 两阶段XSLT转换,首先将字符串转换为XML
    • The question was edited to present a solution using msxml and XmlDocument.loadXml() (this is presumably not directly useful to you) 已对问题进行了编辑,以使用msxml和XmlDocument.loadXml()提出解决方案(可能对您没有直接帮助)
    • The second answer demonstrates using an external XML parser, in this case SAXON, which provides a parse() function accessible to the stylesheet (this relies on SAXON being available on the client, which may not be a safe assumption for you) 第二个答案演示了如何使用外部XML解析器(在本例中为SAXON),该解析器提供了供样式表访问的parse()函数(这依赖于客户端上可用的SAXON,这对您而言可能不是一个安全的假设)

Ultimately, this would be a lot easier for you, and probably more portable and robust, if you could persuade whoever controls the form of the input XML to provide the embedded HTML as actual XHTML fragments rather than text. 最终,如果您可以说服控制输入XML格式的任何人将嵌入式HTML作为实际XHTML片段而不是文本提供,那么对您来说,这将容易得多,并且可能更加便携式和强大。 If indeed that is not an option then you have my sympathy. 如果确实那不是一个选择,那么您对我表示同情。

thanks to all of you to have trying to help me. 多亏了大家能帮助我。 I found the solution : 我找到了解决方案:

function decode() {
  var divs = document.getElementsByTagName('div');
  for(var i = 0; i < divs.length; i++) { 
    var div = divs[i];
    if (div.className == "stx") {
      div.innerHTML = div.firstChild.nodeValue;
    }
  }
}

I hope it will help other peoples. 我希望它将对其他民族有所帮助。 Goodbye :) 再见 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM