Convert XML to XML using XSLT in Firefox and IE

I made a conversion from several XML formats to one standard. My XSL is as follows:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="list | store"> <list> <xsl:for-each select="item | product | product-store"> <item> <name> <xsl:choose> <xsl:when test="name"><xsl:value-of select="substring-before(name, ' ')" /></xsl:when> <xsl:otherwise><xsl:value-of select="name | title" /></xsl:otherwise> </xsl:choose> </name> <desc> <xsl:choose> <xsl:when test="name"><xsl:value-of select="substring-after(name, ' ')" /></xsl:when> <xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise> </xsl:choose> </desc> <nr><xsl:value-of select="index | number" /></nr> </item> </xsl:for-each> </list> </xsl:template> </xsl:stylesheet> 

my XML example

 <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <?xml-stylesheet type="text/xsl" href="transform.xsl"?> <list> <item> <index>1362242627</index> <name>test 22</name> </item> <item> <index>2362625609</index> <name>test 4</name> </item> <item> <index>736274650</index> <name>test 76</name> </item> </list> 

Why is it not showing correctly in browsers like Firefox 17, IE9 and Google Chrome? They display it as plain text, but the return type is "text / xml". It works correctly only in Opera.

+2
source share
2 answers

I think the problem is to say what the β€œright” display is. Browsers such as Firefox or IE assume that after you download an XML document with xml-stylesheet processing instructions like text/xsl into a browser window, you want to convert the XML to what the browser knows for rendering, such as HTML or these days (e.g. X) HTML plus SVG (or plus MathML). However, your stylesheet takes the XML input and converts it to some format of the XML result, which is unknown to the browser, so all it does is display the contents of the text nodes in the result tree. Opera seems to convert the XML input into an XML result, but then it seems to recognize that the format of the result is unknown, and this way decides to display the original result tree. It may be what you prefer, but I'm not sure there is a specification that requires this behavior.

+6
source

The predominance of newlines and tabs works if xsl:output="text" in Firefox and Chrome. Oddly enough, IE ignores text mode indentation. The following is an example stylesheet with a link that demonstrates this:

 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="newline-indent.xml"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" > <!-- Output HTML doctype with text/html content-type and without XML declaration--> <xsl:output method="text" encoding="utf-8" version="1.0" media-type="text/plain" indent="yes" standalone="no" omit-xml-declaration="no"/> <!-- Output the HTML markup--> <xsl:template xml:space="preserve" match="/"> <root> <child>1 &#13;&#10;</child> <child> <grandchild>&#09; 1.1 &#13;&#10;</grandchild> </child> <child> <grandchild> <great-grandchild>&#09; &#09; 1.1.1</great-grandchild> </grandchild> </child> </root> </xsl:template> </xsl:stylesheet> 

The following comment from Mozilla's Error explains why XML serialization does not work for the XML namespace:

The current version of Gecko uses an XML serializer to serialize XHTML content.

Use style tags and literals in tags to format output in IE:

 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="newline-indent-ie.xml"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" > <xsl:output method="xml" encoding="utf-8" version="1.0" media-type="application/xml" indent="yes" standalone="no" omit-xml-declaration="no"/> <xsl:template match="/"> <style>* { white-space:pre-wrap; }</style> <root> <child >1</child> <child> <grandchild >1.1</grandchild> </child> <child> <grandchild> <great-grandchild >1.1.1</great-grandchild> </grandchild> </child> </root> </xsl:template> </xsl:stylesheet> 

References

0
source

Source: https://habr.com/ru/post/917331/


All Articles