Using the browser to transform an XML document - html

I have XML data and am trying to use XSL to format the data. I am following some tutorials. When previewing the XML in Internet Explorer the data is on one line; when previewing with Firefox I get the error message:
Error loading stylesheet: Parsing an XSLT stylesheet failed.
Here the XML:
<?xml version= "1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<countries>
<country>
<countryname>United States</countryname>
</country>
<country>
<countryname>United Kingdom</countryname>
</country>
<country>
<countryname>Deutschland</countryname>
</country>
<country>
<countryname>Osterreich</countryname>
</country>
<country>
<countryname>Espana</countryname>
</country>
<country>
<countryname>France</countryname>
</country>
<country>
<countryname>Italia</countryname>
</country>
<country>
<countryname>China</countryname>
</country>
<country>
<countryname>Hong Kong</countryname>
</country>
<country>
<countryname>Japan</countryname>
</country>
<country>
<countryname>Singapore</countryname>
</country>
<country>
<countryname>Taiwan</countryname>
</country>
<country>
<countryname>Malaysia</countryname>
</country>
</countries>
Here is the XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/countries">
<html>
<body>
<xsl:for-each select="country"
<xsl:value-of select="countryname"/><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylsheet>
Why does the browser not display the XML document as described by the XSL template?
Thank you!

Missing Bracket
<xsl:for-each select="country" must be <xsl:for-each select="country">.
Note the closing >.
Extra Space
Also, you may wish to remove the leading spaces on the first line of the document, if they exist:
<?xml version="1.0" encoding="ISO-8859-1"?>
vs
<?xml version="1.0" encoding="ISO-8859-1"?>
Typo
</xsl:stylsheet> must be </xsl:stylesheet>
After making these changes, the list of countries appear.
Debugging
Consider editing XML and XSL using a text editor that has syntax highlighting and would alert you visually to such errors.

Related

xslt 3.0 xml to JSON number format

I would like to convert the following xml to JSON:
<?xml version='1.0' encoding='utf-8'?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<array key="ids">
<number>3218087</number>
</array>
</map>
using
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:value-of select="xml-to-json(.)" />
</xsl:template>
</xsl:stylesheet>
This gives me:
{"ids":[3.218087E6]}
What I need is
{"ids":[3218087]}
Any help would be highly appreciated.
Cheers
Przemek
The spec https://www.w3.org/TR/xpath-functions/#func-xml-to-json for a number element suggests:
An element $E named number results in the output of the string result
of xs:string(xs:double(fn:string($E)))
and the cast to xs:string requires the E notation for values greater than 1000000 I think (https://www.w3.org/TR/xpath-functions/#casting-to-string).
So I don't think the xml-to-json result can be changed, unless a processor allows some options to choose a different formatting rule for numbers.

Incorrect display when an image is added to XML

This is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="staff.xsl"?>
<st>
<employee>
<name>
<firstname>Clay</firstname>
<lastname>Hansen</lastname>
</name>
<jobtitle>Professor</jobtitle>
<department>Department: Communication and Marketing</department>
<office>Office: CH-H 111</office>
<phone>Phone: (847)257-1234</phone>
<email>Email: clay.h#abc.com</email>
<profile>
<html xmlns="http://www.w3.org/1999/xhtml">
<img src="C:\Users\username\Desktop\im.jpg" /> <!-- added closing tag by edit -->
</html>
</profile>
</employee>
</st>
here is my xml and extrenal xsl code. When I add an image, it does not display my data in tabular form. It displays it like words in a sentence. How can I solve it?
This is because xsl:value-of returns the concatenated text nodes of a element's subtree with the markup removed (in your case there is none). You could copy the img node like this:
<xsl:for-each select="employee">
<td>
<xsl:copy-of select="profile//xhtml:img"></xsl:copy-of>
</td>
</xsl:for-each>
Note, that you have to declare the xhtml namespace in your stylesheet to process the img nodes:
xmlns:xhtml="http://www.w3.org/1999/xhtml"

Retain formatting tags from XML with XSLT

I have to display an XML document using XSLT but I would like apply the <p> and <i> HTML tags from the original XML document. For example p tag from XML would create a new paragraph. What would be the best way to accomplish this?
XML and XSLT code posted below.
Edit: clarified the original question
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="tour.xsl"?>
<cars>
<car>
<description>
<p><i>There is some text here</i> There is some more text here</p>
<p>This should be another paragraph</p>
<p>This is yet another paragraph</p>
</description>
</car>
</cars>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0"/>
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<xsl:value-of select="car"/><p/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>`
your actual question can be answered with a stylesheet like this, which uses apply-templates
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<xsl:apply-templates select="/cars/car/description/*"/>
<p/>
</body>
</html>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
However, if you have multiple cars you probably want to have some headlines. Then a stylesheet like this may point towards that goal:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<xsl:apply-templates select="/cars/car/*"/>
<p/>
</body>
</html>
</xsl:template>
<xsl:template match="description">
<h1>Subtitle</h1>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You can extend your template to copy over the p elements like this:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0"/>
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<xsl:for-each select="/cars/car">
<xsl:for-each select="description/p">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Given your XML input, the above XSLT will produce this output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title</title>
</head>
<body>
<p><i>There is some text here</i> There is some more text here
</p>
<p>This should be another paragraph</p>
<p>This is yet another paragraph</p>
</body>
</html>
Update:
I am trying to run in komodo's built in browser and ie.
You'd have to research komodo's XSLT capabilities. Also, be aware of challenges running XSLT in the browser; better to run on the server or in batch mode and only use the browser to display the results.
That said, the following XML file will open in IE 11 and be styled per the above XSLT sheet (named tour.xml and located in the same directory as the XML file):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='tour.xsl'?>
<cars>
<car>
<description>
<p><i>There is some text here</i> There is some more text here</p>
<p>This should be another paragraph</p>
<p>This is yet another paragraph</p>
</description>
</car>
</cars>
Here's a screenshot of the output in IE 11:
Update 2
I tried the template solution but it still returns unformatted text in
one line.
If IE 11 is given an XML file without a xml-stylesheet PI,
<?xml-stylesheet type='text/xsl' href='tour.xsl'?>
then it will display the XML in an outline form.
If IE 11 is given an XML file with a xml-stylesheet PI and it can find the XSLT file specified, it will apply the XSLT and display the results in the browser. This result can be seen in the above screenshot (and is your desired result).
If IE 11 is given an XML file with a xml-stylesheet PI and it cannot find the XSLT file specified, it will display unformatted text in one line as you describe.
Therefore, focus the search for your problem on the location and name of the XSLT file. Here is what happens when I intentionally force such behavior by renaming the XSLT file,
<?xml-stylesheet type='text/xsl' href='tour_CANNOT_FIND.xsl'?>
so that it cannot be found:
Note: Press F-12 to reveal the console.
Then, if you click on the "Allow blocked content" button:
You see the diagnostic message that tour_CANNOT_FIND.xsl cannot be found. If you resolve the problem of IE not finding your XSLT file, you should then see the formatted results of your XSLT file in the browser.

Auto add all files in the folder

Following part includes a single file but I would like to include all files in the folder which matches with *.log. Is there a way to make it work?
<!DOCTYPE root
[
<!ENTITY log SYSTEM "Log.log">
]>
<root>
&log;
</root>
*.log
There is no root element in log files.
not even <?xml version="1.0" encoding="UTF-8"?> and/or xslt link like <?xml-stylesheet type="text/xsl" href="test.xsl" ?> each file has 1000+ records like below
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<SubType Name="Information">0</SubType>
<Correlation ActivityID="a54221e2-ad37-434a-8f0d-101f7abc2221" />
</System>
<ApplicationData>Test21</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<SubType Name="Information">0</SubType>
<Correlation ActivityID="d30741c2-da73-434a-8f0d-101f7ceb2228" />
</System>
<ApplicationData>Test24</ApplicationData>
</E2ETraceEvent>

HTML entity numbers in xslt

I'm attempting to transform HTML to XML. My Input HTML is obtained dynamically, and the input HTML has html entity numbers as below.
HTML Input:
<root>
<h1>Hello stack Over flow</h1>
<H1 align="left">The list will be managed with a  <SUB>of © ®</H1>
</root>
My transform looks as below :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="root">
<xsl:copy >
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
the output from the transform is writing all html entity numbers as html special characters.
The desired output should have html entity numbers instead of html characters. Please help me to get out of this issue?
You could try to put encoding="US-ASCII" on your xsl:output directive, that way any characters outside of that encoding should be output as character references.