XML / XSLT - XML Parsing Error: no element found - html

When i open the xml file with mozilla i get this error:
XML Parsing Error: no element found
Location: file:///D:/PTI/xsl.xsl
Line Number 26, Column 8:
-------^
How can i fix that ?
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<books name="List">
<book>
<title>Don Quixote</title>
<author>Miguel de Cervantes</author>
<theme>Adventure</theme>
<price>$120</price>
<year>1605</year>
</book>
<book>
<title>A Tale Of Two Cities</title>
<author>Charles Dickens</author>
<theme>History</theme>
<price>$75</price>
<year>1859</year>
</book>
</books>
XSL Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Book Catalog</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Theme</th>
<th>Price</th>
<th>Year</th>
</tr>
<xsl:for-each select="books/book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="theme"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>

Unless you forget to include a part of your XSLT file you're missing some closing tags (template and stylesheet). Just add them to the end of your xsl.xsl document:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
...your stuff here...
</html>
</xsl:template>
</xsl:stylesheet>

Related

How to add clickable link to XML?

I am trying to make a URL in my XML file clickable. Below is my XML and XSL files which work fine together.
I've tried using XLink and href in XML file but it didn't work.
How do I make my URL in the XML file clickable?
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file.xsl" ?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="1.xsl" ?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Johnny</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<url>http://www.yahoo.com</url>
</cd>
</catalog>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>XSL</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Country</th>
<th style="text-align:left">Company</th>
<th style="text-align:left">Price</th>
<th style="text-align:left">URL</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="country"/></td>
<td><xsl:value-of select="company"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="url"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Before writing any XSLT, you should really know what output you want. And to make a link clickable in HTML you can do this...
http://www.yahoo.com
So, in your XSLT, instead of doing this....
<td><xsl:value-of select="url"/></td>
Do this...
<td>
<a href="{url}">
<xsl:value-of select="url"/>
</a>
</td>
Note the use of curly braces in the href attribute. This is known as an Attribute Value Template. The curly braces indicate an expression to be evualated, rather than output literally.

XPath doesn't recognize path

I'm writing an XSL transformer to convert XML to HTML. Here's my gun.xml:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="guns.xslt"?>
<guns xsi:noNamespaceSchemaLocation="guns2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<gun>
<model>revolver</model>
<handy>1</handy>
<origin>Britain</origin>
<ttc>20mm</ttc>
</gun>
</guns>
And guns.xslt here:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Gun Collection</h2>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Model</th>
<th>Origin</th>
<th>TTC</th>
</tr>
<xsl:for-each select="guns/gun">
<tr>
<td><xsl:value-of select = "model"/></td>
<td><xsl:value-of select = "handy"/></td>
<td><xsl:value-of select = "origin"/></td>
<td><xsl:value-of select = "ttc"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Both files are located in the same package, but XPAth doesn't recognize "guns/gun" in for-each block. What am I missing?
You have declared in xml-stylesheet - type as "text/xsl", please change file extension from "guns.xslt" to "guns.xsl" and also change in your XML as below:
<?xml-stylesheet type="text/xsl" href="guns.xsl"?>
Then your result will be as below:

XSLT code returns empty rows when converting XML to HTML table

I am trying to convert an XML document to HTML using XSLT 1.0. I aim to display the XML rows in an HTML table. But my XSLT code returns empty rows in the HTML table. Here's what my XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person id="ABCDE1234" firstname="John" lastname="Lewis" sex="male"/>
<person id="XYZWU6789" firstname="Marie" lastname="Claire" sex="female"/>
</people>
Here's my XSLT code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>XML to HTML using XSLT</title>
</head>
<body>
<table border="1" >
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>sex</th>
</tr>
<xsl:for-each select="people/person">
<tr>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="sex"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
However, my XSLT code runs properly when I reformat the XML file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person>
<id>ABCDE1234</id>
<firstname>John</firstname>
<lastname>Lewis</lastname>
<sex>Male</sex>
</person>
<person>
<id>XYZWU6789</id>
<firstname>Mary</firstname>
<lastname>Claire</lastname>
<sex>Female</sex>
</person>
</people>
Why is this happening? Any ideas?
Just add the # sign so that the values will recognize as attributes.
<td><xsl:value-of select="#id"/></td>
<td><xsl:value-of select="#firstname"/></td>
<td><xsl:value-of select="#lastname"/></td>
<td><xsl:value-of select="#sex"/></td>

Browsers don't parse stylesheet properly

I have an XML File:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet hred="remakes.xsl" type="text/xsl" ?>
<remakes>
<remake>
<rtitle>Pygmalion</rtitle>
<ryear>1938</ryear>
<fraction>0.5</fraction>
<stitle>Pygmalion</stitle>
<syear>1937</syear>
</remake>...
and I have created a stylesheet:
?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/remakes">
<html>
<body>
<table border="1">
<tr>
<th>rtitle<</th>
<th>fraction<</th>
<th>stitle<</th>
<th>syear<</th>
</tr>
<xsl:for-each select="remake">
<xsl:value-of select="rtitle"/></td>
<xsl:value-of select="fraction"/></td>
<xsl:value-of select="stitle"/></td>
<xsl:value-of select="syear"/></td>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
But browsers don't parse it so instead of a table there is chaos. Is there a mistake in the code?
There are a number of problems with your stylesheet, although they could always be typos with your question, but as there are too many to mention in comments, they are as follows:
The namespace prefix xsl has not been bound. You should type xmlns:xsl="..."
There is a < symbol in each table header <th>rtitle<</th>. If you really wanted this, you should write it as <th>rtitle<</th>, but more likely it should just be <th>rtitle</th>
You have a closing <\td> for each row, but no opening <td> tag.
You also really need to wrap those <td> in a <tr> tag for it to be rendered properly.
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/remakes">
<html>
<body>
<table border="1">
<tr>
<th>rtitle</th>
<th>fraction</th>
<th>stitle</th>
<th>syear</th>
</tr>
<xsl:for-each select="remake">
<tr>
<td><xsl:value-of select="rtitle"/></td>
<td><xsl:value-of select="fraction"/></td>
<td><xsl:value-of select="stitle"/></td>
<td><xsl:value-of select="syear"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

online xml to html parser

I am looking for a online program that parses any xml to html. For instance, if I have the following xml layout
<users>
<user>
</name>john</name>
<pictures>
<pic1>URL of picture1.png</pic1>
<pic2>URL of picture2.png</pic2>
</pictures>
</user>
<user>
</name>mary</name>
<pictures>
<pic1>URL of picture1.png</pic1>
<pic2>URL of picture2.png</pic2>
</pictures>
</user>
</users>
it can create an html page displaying the content of the nodes and give some minimal formatting for easy reading. Any such tool?
Edit: I just gave an example, I want a generic tool that can parse any xml without knowing its structure beforehand.
One way to tackle this would be to apply an XSLT transformation.
You'd need to create an XSL stylesheet, something like...
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Contacts</title>
</head>
<body>
<xsl:for-each select="//user">
<h2><xsl:value-of select="name" /></h2>
<p><xsl:value-of select="pictures/pic1" /></p>
<p><xsl:value-of select="pictures/pic2" /></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Then link to it at the top of your xml file. In the example below the second line links to the stylesheet named 'myStyleSheet.xsl'.
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>
<users>
<user>
<name>john</name>
<pictures>
<pic1>URL of picture1.png</pic1>
<pic2>URL of picture2.png</pic2>
</pictures>
</user>
<user>
<name>mary</name>
<pictures>
<pic1>URL of picture1.png</pic1>
<pic2>URL of picture2.png</pic2>
</pictures>
</user>
</users>
You could try this.
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>