var myDocfragment = xsltProcessor.transformToFragment(xmlDoc, document); 

myDocfragment - is an object of type - document-fragment. How can I get all the tags with values ​​as text? I want to transfer it here - document.write (...)

Here is my code:

 var res_xml = json2xml(o_rows, o_cols); var xslStylesheet; var myXMLHTTPRequest = new XMLHttpRequest(); myXMLHTTPRequest.open("GET", "xsl/rep_list.xsl", false); myXMLHTTPRequest.send(null); var xsltProcessor = new XSLTProcessor(); xslStylesheet = myXMLHTTPRequest.responseXML; xsltProcessor.importStylesheet(xslStylesheet); var myDocFragment = xsltProcessor.transformToFragment(res_xml, document); var wnd_list=window.open("", "wnd_list"); wnd_list.document.body.appendChild(myDocFragment); 

 function json2xml(obj_json, a_fields) { var doc = $.parseXML("<root/>"); var xml = doc.getElementsByTagName("root")[0]; var elem, a_cnt, s_row, s_str; var gr_rows= doc.createElement('rowset'); for (key in obj_json) { if (obj_json.hasOwnProperty(key)) { el_row = doc.createElement('row'); ochild=obj_json[key]; for (key2 in ochild) { if ($.isEmptyObject(a_fields)) { a_cnt=[0]; } else { a_cnt=$.grep(a_fields , function (element, index) { return element.field == key2; }); } if (a_cnt.length>0){ elem = doc.createElement(key2); s_str=ochild[key2]; $(elem).text(s_str); el_row.appendChild(elem); } }; gr_rows.appendChild(el_row); } } xml.appendChild(gr_rows); return xml; } 

Example res_xml:

 <?xml version="1.0" encoding="UTF-8"?> <root> <rowset> <row> <recid>3</recid> <date_reg>13.09.2016 17:20:48</date_reg> <rep_type>Отчет по расходам на содержание жилья</rep_type> <creator>Омаров Габит </creator> <rep_stat>Создан</rep_stat> </row> </rowset> <colset> <col>Номер</col> <col>Дата</col> <col>Наименование</col> <col>Автор</col> <col>Статус</col> </colset> </root> 

Example rep_list.xsl:

 <?xml version="1.0" encoding="UTF-8"?> <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <body> <table border="1" cellspacing="0"> <tr> <xsl:for-each select="root/colset/col"> <th align="center"><xsl:value-of select="."/></th> </xsl:for-each> </tr> <xsl:for-each select="root/rowset/row"> <tr> <xsl:for-each select="*"> <td><xsl:value-of select="."/></td> </xsl:for-each> </tr> </xsl:for-each> </table> </body> </html> 

I posted a demonstration:

  1. Open the resource https://demo.topksk.kz
  2. Enter:
    login: gabit.omarov@gmail.com pass: orapas $ 123

  3. Insert url: https://demo.topksk.kz/reports.html and press Enter.

  4. "Type of application" - select "Report on housing costs".
  5. Click on the button - "Search"
  6. In the tabular part, click on the button - "List display"
  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

2 answers 2

document.write better not to use document.write in this case, since a fragment is an object, when inserted into the DOM, all its contents are inserted without the immediate element of the fragment.

Therefore, to insert its contents on the page, you need to use, for example, appendChild , on that element where you want to insert, or insertBefore .

For example:

 var myDocfragment = xsltProcessor.transformToFragment(xmlDoc, document); document.body.appendChild(myDocfragment); 
  • I tried it. The display problem occurs in Firefox. Probably due to the fact that: DocumentFragment does not have the usual properties of DOM nodes, such as innerHTML, tagName, etc. This is not a node. I need to get the contents of myDocfragment with tags. How to do it ? - Gabit Omarov
  • @GabitOmarov, DocumentFragment - is not a node and is not inserted into the DOM, only its contents are inserted, so it cannot cause a display error in FireFox. Obviously you just need to check the styles. - Grundy
  • Style have an XSL file? I use the same file for everyone, it is on the application server. - Gabit Omarov
  • @GabitOmarov, I mean css - which is responsible for the display. If after inserting content into an element, something is shown wrong, then it does not set the necessary css-styles. If you're still trying to use document.write, it won't work. Use appendChild, or insertBefore, as I indicated in the answer - Grundy
  • here is my code: [code] var res_xml = json2xml (o_rows, o_cols); var xslStylesheet; var myXMLHTTPRequest = new XMLHttpRequest (); myXMLHTTPRequest.open ("GET", "xsl / rep_list.xsl", false); myXMLHTTPRequest.send (null); var xsltProcessor = new XSLTProcessor (); xslStylesheet = myXMLHTTPRequest.responseXML; xsltProcessor.importStylesheet (xslStylesheet); var myDocFragment = xsltProcessor.transformToFragment (res_xml, document); var wnd_list = window.open ("", "wnd_list"); wnd_list.document.body.appendChild (myDocFragment); [/ code] - Gabit Omarov

Instead of this line:

 var myDocFragment = xsltProcessor.transformToFragment(res_xml, document); 

Put it like this:

 doc=$.parseXML(res_xml.outerHTML); var myDocFragment = xsltProcessor.transformToFragment(doc, document);