Javascript 在 HTML 或其他 XML 中嵌入 XML(带有 XML 声明)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33463269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:05:12  来源:igfitidea点击:

Embedding XML (with XML declaration) in HTML or other XML

javascripthtmlxmlprocessing-instruction

提问by Irshu

I have the error :

我有错误:

Error Parsing /WEB-INF/includes/VerDatosProyeccion.xhtml: Error Traced[line: 4302] The processing instruction target matching "[xX][mM][lL]" is not allowed.

Error Parsing /WEB-INF/includes/VerDatosProyeccion.xhtml: Error Traced[line: 4302] 不允许匹配“[xX][mM][lL]”的处理指令目标。

I am using the code of same code of : http://jsfiddle.net/qxLn3h86/. I cut the code and past into my code.

我正在使用相同代码的代码:http: //jsfiddle.net/qxLn3h86/。我将代码剪切并过去放入我的代码中。

My code looks like this :

我的代码如下所示:

        <table id="tbl1">


<tr>
    <td>Name</td>
    <td>Birthday</td>
    <td>Amount</td>
    <td>Rebate (10%)</td>
  </tr>
  <tr>
    <td>Smith</td>
    <td data-type="DateTime" data-style="Date" data-value="1980-03-23">Mar 23 1980</td>
    <td data-type="Number" data-style="Currency" data-value="1234.56">$ 1,234.56</td>
    <td data-formula="=RC[-1]/10" data-type="Number" data-style="Currency">$ 123.45</td>
  </tr>
  <tr>
    <td>Doe</td>
    <td data-type="DateTime" data-style="Date" data-value="1978-11-05">Nov 05 1978</td>
    <td data-type="Number" data-style="Currency" data-value="2345.67">$ 2,345.67</td>
    <td data-formula="=RC[-1]/10" data-type="Number" data-style="Currency">$ 234.56</td>
  </tr>
</table>

<table id="tbl2">
  <tr>
    <td>Product</td>
    <td>Price</td>
    <td>Available</td>
    <td>Count</td>
  </tr>
  <tr>
    <td>Bred</td>
    <td data-type="Number" data-style="Currency" data-value="1.89">$ 1.89</td>
    <td data-type="Boolean" data-value="1">yes</td>
    <td data-type="Number" data-value="123">123</td>
  </tr>
  <tr>
    <td>Butter</td>
    <td data-type="Number" data-style="Currency" data-value=".89">$ .89</td>
    <td data-type="Boolean" data-value="0">no</td>
    <td data-type="Number" data-value="0">0</td>
  </tr>
</table>


            <button  onclick="tablesToExcel(['tbl1','tbl2'], ['Customers','Products'], 'TestBook.xls', 'Excel')">Export to Excel</button>

</h:panelGroup>


<script>
    function exportarExcel(){
        $("#tableProyeccion").table2excel({
            exclude: ".excludeThisClass",
            name: "Worksheet Name",
            filename: "Proyeccion" //do not include extension
        });
    }
    </script>
<script>
    var tablesToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
        , tmplWorkbookXML ='<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
          + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'
          + '<Styles>'
          + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'
          + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'
          + '</Styles>' 
          + '{worksheets}</Workbook>'
        , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
        , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'
        , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
        return function(tables, wsnames, wbname, appname) {
          var ctx = "";
          var workbookXML = "";
          var worksheetsXML = "";
          var rowsXML = "";

          for (var i = 0; i < tables.length; i++) {
            if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
            for (var j = 0; j < tables[i].rows.length; j++) {
              rowsXML += '<Row>'
              for (var k = 0; k < tables[i].rows[j].cells.length; k++) {
                var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
                var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
                var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
                dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
                var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
                dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null;
                ctx = {  attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':''
                       , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String'
                       , data: (dataFormula)?'':dataValue
                       , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':''
                      };
                rowsXML += format(tmplCellXML, ctx);
              }
              rowsXML += '</Row>'
            }
            ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i};
            worksheetsXML += format(tmplWorksheetXML, ctx);
            rowsXML = "";
          }

          ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
          workbookXML = format(tmplWorkbookXML, ctx);

    console.log(workbookXML);

          var link = document.createElement("A");
          link.href = uri + base64(workbookXML);
          link.download = wbname || 'Workbook.xls';
          link.target = '_blank';
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
      })();

</script>

回答by kjhughes

General answer

一般回答

When XML declarations (<?xml version="1.0" ?>) appear anywhere other than at the very top of an XML document, parsers often confuse them with processing instructions and issue misleading error messages.

当 XML 声明 ( <?xml version="1.0" ?>) 出现在 XML 文档最顶部以外的任何地方时,解析器通常会将它们与处理指令混淆并发出误导性的错误消息。

The canonical answerfor fixing your error,

修复错误的规范答案

Error Traced[line: 4302] The processing instruction target matching "[xX][mM][lL]" is not allowed.

Error Traced[line: 4302] 不允许匹配“[xX][mM][lL]”的处理指令目标。

and all similar errors related to XML declarations is covered by this Q/A:

以及与 XML 声明相关的所有类似错误都包含在此 Q/A 中:

There you'll find solutions to the three causes of such errors:

在那里,您将找到解决此类错误的三个原因:

  1. Visible content before the XML declaration
  2. Invisible content before the XML declaration
  3. Duplicate XML declarations
  1. XML 声明前的可见内容
  2. XML 声明前的不可见内容
  3. 重复的 XML 声明


Suggestions for your particular case

针对您的特殊情况的建议

Trying wrapping your code within scriptwith CDATA:

尝试scriptCDATA以下代码包装您的代码:

<script>
//<![CDATA[
    ...code containing XML declaration (`<?xml version="1.0"?>`)
//]]>
</script>

so that the XML declaration isn't interpreted to be part of the enclosing document. XML declarations can only appear at the very top of an XML document (and there can only be one of them at most).

这样 XML 声明就不会被解释为封闭文档的一部分。XML 声明只能出现在 XML 文档的最顶部(并且最多只能有一个)。

If that doesn't resolve your problem, examine where you're outputting the XML declaration. Make sure that there is no visible or invisible content ahead of the XML declaration, and make sure that there are not multiple XML declarations in the output. For more details see:

如果这不能解决您的问题,请检查您输出 XML 声明的位置。确保 XML 声明之前没有可见或不可见的内容,并确保输出中没有多个 XML 声明。有关更多详细信息,请参阅:

回答by Irshu

This can also happen if you have duplicate xml tag <?xml version="1.0"...>defined accidentally

如果您<?xml version="1.0"...>意外定义了重复的 xml 标记,也会发生这种情况