XSTLProcessor bypass and tags
I have a XML and XSL file in client and want to transform it to a HTMLElement so that I can append to my current element
But the result is far from what I expected. Every seems fine except the <td>
and <tr>
tags is excluded in the HtmlElement result
My XML file
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
My XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My javascript code that used to transform and append result
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
My html result render
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
I believe this is because of the XSLTProcessor that I use to transform, because I tried to transform it with my IDE's XSLT tool, and the result is perfectly correct
javascript html xml xslt transformation
add a comment |
I have a XML and XSL file in client and want to transform it to a HTMLElement so that I can append to my current element
But the result is far from what I expected. Every seems fine except the <td>
and <tr>
tags is excluded in the HtmlElement result
My XML file
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
My XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My javascript code that used to transform and append result
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
My html result render
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
I believe this is because of the XSLTProcessor that I use to transform, because I tried to transform it with my IDE's XSLT tool, and the result is perfectly correct
javascript html xml xslt transformation
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTMLtable
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?
– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence oftr
elements is asking for trouble.
– Martin Honnen
Nov 13 '18 at 14:24
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTMLtr
elements to be inserted into atbody
is troublesome.
– Martin Honnen
Nov 13 '18 at 16:14
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTMLtr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.
– Martin Honnen
Nov 13 '18 at 16:17
add a comment |
I have a XML and XSL file in client and want to transform it to a HTMLElement so that I can append to my current element
But the result is far from what I expected. Every seems fine except the <td>
and <tr>
tags is excluded in the HtmlElement result
My XML file
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
My XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My javascript code that used to transform and append result
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
My html result render
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
I believe this is because of the XSLTProcessor that I use to transform, because I tried to transform it with my IDE's XSLT tool, and the result is perfectly correct
javascript html xml xslt transformation
I have a XML and XSL file in client and want to transform it to a HTMLElement so that I can append to my current element
But the result is far from what I expected. Every seems fine except the <td>
and <tr>
tags is excluded in the HtmlElement result
My XML file
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
My XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My javascript code that used to transform and append result
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
My html result render
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
I believe this is because of the XSLTProcessor that I use to transform, because I tried to transform it with my IDE's XSLT tool, and the result is perfectly correct
javascript html xml xslt transformation
javascript html xml xslt transformation
edited Nov 13 '18 at 14:52
Ziggy192
asked Nov 13 '18 at 13:49
Ziggy192Ziggy192
656
656
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTMLtable
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?
– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence oftr
elements is asking for trouble.
– Martin Honnen
Nov 13 '18 at 14:24
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTMLtr
elements to be inserted into atbody
is troublesome.
– Martin Honnen
Nov 13 '18 at 16:14
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTMLtr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.
– Martin Honnen
Nov 13 '18 at 16:17
add a comment |
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTMLtable
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?
– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence oftr
elements is asking for trouble.
– Martin Honnen
Nov 13 '18 at 14:24
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTMLtr
elements to be inserted into atbody
is troublesome.
– Martin Honnen
Nov 13 '18 at 16:14
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTMLtr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.
– Martin Honnen
Nov 13 '18 at 16:17
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTML
table
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?– Martin Honnen
Nov 13 '18 at 14:24
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTML
table
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence of
tr
elements is asking for trouble.– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence of
tr
elements is asking for trouble.– Martin Honnen
Nov 13 '18 at 14:24
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTML
tr
elements to be inserted into a tbody
is troublesome.– Martin Honnen
Nov 13 '18 at 16:14
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTML
tr
elements to be inserted into a tbody
is troublesome.– Martin Honnen
Nov 13 '18 at 16:14
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTML
tr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.– Martin Honnen
Nov 13 '18 at 16:17
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTML
tr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.– Martin Honnen
Nov 13 '18 at 16:17
add a comment |
1 Answer
1
active
oldest
votes
With current versions of Firefox, Chrome and Edge on Windows 10 1803 it works for me to have XSLT return a fragment with XHTML tr
elements to then be inserted into the hosting HTML document and a tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
@Ziggy192, well, try yourself, in part (the methodhtml
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output methodhtml
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.
– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence oftr
elements is parsed as HTML you see that thetr
andtd
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of contexttr
/td
elements. If you switch to output methodxml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.
– Martin Honnen
Nov 14 '18 at 10:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282501%2fxstlprocessor-bypass-tr-and-td-tags%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With current versions of Firefox, Chrome and Edge on Windows 10 1803 it works for me to have XSLT return a fragment with XHTML tr
elements to then be inserted into the hosting HTML document and a tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
@Ziggy192, well, try yourself, in part (the methodhtml
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output methodhtml
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.
– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence oftr
elements is parsed as HTML you see that thetr
andtd
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of contexttr
/td
elements. If you switch to output methodxml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.
– Martin Honnen
Nov 14 '18 at 10:41
add a comment |
With current versions of Firefox, Chrome and Edge on Windows 10 1803 it works for me to have XSLT return a fragment with XHTML tr
elements to then be inserted into the hosting HTML document and a tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
@Ziggy192, well, try yourself, in part (the methodhtml
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output methodhtml
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.
– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence oftr
elements is parsed as HTML you see that thetr
andtd
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of contexttr
/td
elements. If you switch to output methodxml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.
– Martin Honnen
Nov 14 '18 at 10:41
add a comment |
With current versions of Firefox, Chrome and Edge on Windows 10 1803 it works for me to have XSLT return a fragment with XHTML tr
elements to then be inserted into the hosting HTML document and a tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
With current versions of Firefox, Chrome and Edge on Windows 10 1803 it works for me to have XSLT return a fragment with XHTML tr
elements to then be inserted into the hosting HTML document and a tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1</value>
<value>foo 2</value>
<value>foo 3</value>
</item>
<item>
<value>bar 1</value>
<value>bar 2</value>
<value>bar 3</value>
</item>
</root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
answered Nov 13 '18 at 16:43
Martin HonnenMartin Honnen
111k65976
111k65976
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
@Ziggy192, well, try yourself, in part (the methodhtml
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output methodhtml
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.
– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence oftr
elements is parsed as HTML you see that thetr
andtd
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of contexttr
/td
elements. If you switch to output methodxml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.
– Martin Honnen
Nov 14 '18 at 10:41
add a comment |
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
@Ziggy192, well, try yourself, in part (the methodhtml
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output methodhtml
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.
– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence oftr
elements is parsed as HTML you see that thetr
andtd
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of contexttr
/td
elements. If you switch to output methodxml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.
– Martin Honnen
Nov 14 '18 at 10:41
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
what'd happen if you set method='html' in the <xsl:output method="xml" indent="yes"/> ?
– Ziggy192
Nov 14 '18 at 6:58
1
1
@Ziggy192, well, try yourself, in part (the method
html
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output method html
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.– Martin Honnen
Nov 14 '18 at 9:13
@Ziggy192, well, try yourself, in part (the method
html
) I tried that in jsfiddle.net/bycptmL2/3 (I did not use the XHTML namespace there as that doesn't seem to make sense together with output method html
in XSLT 1) and that fiddle, as pointed out earlier in a comment, did not work as needed in Chrome.– Martin Honnen
Nov 14 '18 at 9:13
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
Thank you @Martin! It works with the XHTML namespace and XML method. There's no document mentioned about my case. Should I consider this a bug of XSTLProcessor, or there's something wrong with my approach from the beginning ? Thanks again for your help, deeply appreciate it
– Ziggy192
Nov 14 '18 at 10:10
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
There is no detailed spec on XSLTProcessor, it is just an API Mozilla introduced when it tried to integrate their own XSLT 1.0 implementation into their browser. Other followed the API, to ensure compatibility at the API level, but compatibility at finer levels has never worked well as the architectures of the XSLT integration are too different, as far as I understand it.
– Martin Honnen
Nov 14 '18 at 10:37
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence of
tr
elements is parsed as HTML you see that the tr
and td
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of context tr
/td
elements. If you switch to output method xml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.– Martin Honnen
Nov 14 '18 at 10:41
If you look at jsfiddle.net/bycptmL2/5 where no XSLT is used but just a sequence of
tr
elements is parsed as HTML you see that the tr
and td
elements are dropped as well, so not any snippet of HTML works well as a standalone fragment. That is, as as I guess, the main problem here, with your original approach browsers like Chrome take the result of XSLT as a string and feed it to the HTML parser which drops the out of context tr
/td
elements. If you switch to output method xml
and use the XHTML namespace then you don't have the HTML parser in between dropping elements.– Martin Honnen
Nov 14 '18 at 10:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282501%2fxstlprocessor-bypass-tr-and-td-tags%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does that problem occur with a particular browser or with all supporting XSLTProcessor? Can you reduce the problem to a minimum and insert it as an executable code snippet? Have you tried to have the XSLT create a complete HTML
table
and then to use Javascript to replace the existing table with the result fragment from the XSLT execution?– Martin Honnen
Nov 13 '18 at 14:24
I am currently not sure what causes the problem but I guess due to the different ways browsers use XSLT (i.e. Firefox doing node to node transformation, Chrome using libxslt to serialize/parse and perhaps serialize again) trying to create a sequence of
tr
elements is asking for trouble.– Martin Honnen
Nov 13 '18 at 14:24
I tried with different browsers and the result is still the same sadly :(
– Ziggy192
Nov 13 '18 at 14:54
I tried an example jsfiddle.net/bycptmL2/3 and it works in Firefox for me, not in Chrome. As I said, while both have the same API with XSLTProcessor and transformToFragment, I think their implementation approach, not to say, their whole architecture to integrate XSLT in the browser, differs vastly as far as I know, so that whole idea to to to have XSLT return a fragment with HTML
tr
elements to be inserted into atbody
is troublesome.– Martin Honnen
Nov 13 '18 at 16:14
On the other hand, jsfiddle.net/bycptmL2/4, which only changes the XSLT to output XHTML
tr
element nodes, works in both Firefox and Chrome for me. Not sure about Edge, doesn't seem to work at all with that fiddle.– Martin Honnen
Nov 13 '18 at 16:17