I've got a simple xml document main.xml
<?xml version="1.0"?>
<students>
<student>
<name>Name1</name>
<surname>Surname1</surname>
</student>
<student>
<name>Name2</name>
<surname>Surname2</surname>
</student>
<student>
<name>Name3</name>
<surname>Surname3</surname>
</student>
</students>
Also I've got a html page main.html where I want to show data from xml file.
<html>
<head>
<title>Test XML</title>
</head>
<body>
<xml id="students" src="main.xml"></xml>
<table id="table" datasrc="#students" border="1">
<thead>
<th>Student name</th>
<th>Student surname</th>
</thead>
<tbody>
<td><span datafld="name"></span></td>
<td><span datafld="surname"></span></td>
</tbody>
</table>
</body>
</html>
But it doesn't work - It shows only head of the table. Also both files are in the same folder. Why?
I'm using Windows 7 and IE8
The case is solved: I forgot tag tr before tds.
Related
**from this type of xml code (program will take this type of xml code as input)**
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="bookstore.xsl"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
I want my program to return this html file for corresponding XML file above
<html>
<body>
<table>
<tbody>
<tr>
<th>Bookstore</th>
<th>Book</th>
<th>title</th>
<th>author</th>
<th>year</th>
<th>price</th>
</tr>
<tr>
<td></td>
<td></td>
<td>Everyday Italian</td>
<td>Giada De Laurentiis</td>
<td>2005</td>
<td>30.00</td>
</tr>
<tr>
<td></td>
<td></td>
<td>Learning XML</td>
<td>Erik T. Ray</td>
<td>2003</td>
<td>39.95</td>
</tr>
</tbody>
</table>
</body>
</html>
do not need to consider all other type of xml file , if the file does not contain a table it will call yyerror from yyparse . There can be more than one table in the code and a table can contain another table .
Am trying to generating PDF out of XML document. Please find my below XML and XSL for the same.
Am expecting it should display all rows under tag but am getting only very first element (rows) in each tag.
Please find my below xml
<receipt>
<order>
<page></page>
<page>
<line_number>1</line_number>
<product_code>S10</product_code>
<line_number>2</line_number>
<product_code>S20</product_code>
<line_number>3</line_number>
<product_code>S92</product_code>
</page>
<page>
<line_number>6</line_number>
<product_code>S92</product_code>
<line_number>7</line_number>
<product_code>S31</product_code>
<line_number>8</line_number>
<product_code>S31</product_code>
</page>
</order>
</receipt>
Please find my xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/receipt">
<html>
<head>
<style>#page {size: a4 landscape;}</style>
</head>
<body>
<table >
<thead>
<tr >
<th >Line</th>
<th>Item Code</th>
</tr>
</thead>
<xsl:for-each select="order/page" >
<tbody>
<tr style="font-size: 9px; ">
<td ><xsl:value-of select="line_number" /></td>
<td ><xsl:value-of select="product_code" /></td>
</tr>
</tbody>
</xsl:for-each>
</table>
<br />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
In the output only 1st element in tag is coming instead of all the elements (rows) under each tag.
for example :
output :
1 s10
6 s92
Expected Output
1 s10
2 s20
3 s92
6 s92
7 s31
8 s31
You want to output one row per line_number, rather that one row per page, so you xsl:for-each needs to select these line_number elements
<xsl:for-each select="order/page/line_number">
Then to get the value of the line_number and following product_code, do this...
<td><xsl:value-of select="." /></td>
<td><xsl:value-of select="following-sibling::product_code[1]" /></td>
Try this...
<xsl:template match="/receipt">
<html>
<head>
<style>#page {size: a4 landscape;}</style>
</head>
<body>
<table >
<thead>
<tr>
<th>Line</th>
<th>Item Code</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="order/page/line_number">
<tr style="font-size: 9px; ">
<td><xsl:value-of select="." /></td>
<td><xsl:value-of select="following-sibling::*[1][self::product_code]" /></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<br />
</body>
</html>
</xsl:template>
Note that this does make the assumption that each line_number will be followed by a product_code.
(I have also moved the creation of the tbody element outside the xsl:for-each as you should really only have one such element in your table, rather than one for each row)
I want to know how I can retrive the url in "supervisor" to use it as a link. I have this xml file.
<pr>
<meta>
<id>AAAAAAAAAAAA</id>
<title>AAAAAAAA</title>
<subtitle>BBBBB</subtitle>
<bdate>OCCCCCCCC</bdate>
<edate>DDDDDDDDDD</edate>
<supervisor url="http://wwww.teacherwWEbpage.com">teacher NAME
</meta>
</pr>
</meta>
<team>
<student>
<name>Rhghfghf</name>
<nr>fghfgh5</nr>
<email>hgghfht</email>
</student>
</team>
<abstract>
</abstract>
<deliverables>
<deliverable url="."></deliverable>
</deliverables>
And this xsl, im trying to put the XML information on a table, which is fine, but now I'm unable to set the teachers name as a link to the address in the url attribute...
How can I use that attribute as the link on the resultant HTML file?
</supervisor>
</meta>
<team>
<student>
<name>hrhshs</name>
<nr>48648</nr>
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"></meta>
</head>
<body>
<h1 align="center"> Projecto Record</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="meta">
<table>
<tr >
<td width="50%"><b>name key:</b><xsl:value-of select="id"/></td>
<td><b>Begin date:</b><xsl:value-of select="bdate"/></td>
</tr>
<tr>
<td><b>Title:</b><xsl:value-of select="title"/></td>
<td><b>End date:</b><xsl:value-of select="edate"/></td>
</tr>
<tr>
<td><b>Subtitle:</b><xsl:value-of select="subtitle"/></td>
<td><b>Supervisor:</b><a href="./supervisor/{#url}" ><xsl:value-of select="supervisor"/> </a></td>
</tr>
</table>
</xsl:template>
You are not far off with your current attempt, but the syntax you want is this...
<a href="{supervisor/#url}" >
I am trying to process an XML file with XSLT to produce a list or table or (eventually) an SQL INSERT command with some of the values. I have been using the example from w3schools http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog.
My XML is very simple and I need to extract just the name and the rate of the hotel:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getHotelsResponse xmlns="http://hotel.booking.vbooking.com">
<getHotelsReturn>
<address>
<number>589-591</number>
<postcode>08014</postcode>
<region>Catalonia</region>
<street>Carrer Vermell</street>
<town>Barcelona</town>
</address>
<name>Downtown Hotel</name>
<rate>235.0</rate>
</getHotelsReturn>
</getHotelsResponse>
</soapenv:Body>
</soapenv:Envelope>
The best XSLT I could build up from the w3schools was this one:
<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpyΠ-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/soapenv:Envelope/soapenv:Body/getHotelsResponse">
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<xsl:for-each select="getHotelsReturn">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="rate"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Which should produce a similar result to what I had in the w3schools example, but it comes back as blank.
Can anyone please explain this? Thanks.
As pointed out by #keshlam, if elements in your input XML have namespaces, you must declare them in your XSLT stylesheet as well.
Find more detail on namespaces in a previous answer of mine.
The gist of the information you find there is: as far as the XSLT processor is concerned, a getHotelsReturn element is entirely different from a vb:getHotelsReturn element.
Stylesheet
<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:vb="http://hotel.booking.vbooking.com"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soapenv vb">
<xsl:template match="/soapenv:Envelope/soapenv:Body/vb:getHotelsResponse">
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<xsl:for-each select="vb:getHotelsReturn">
<tr>
<td><xsl:value-of select="vb:name"/></td>
<td><xsl:value-of select="vb:rate"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<tr>
<td>Downtown Hotel</td>
<td>235.0</td>
</tr>
</table>
</body>
</html>
Rendered HTML
You can use XPaths like this:
/Product1/Product2/Product3[#ValidYN = 'Y' and #ProductType = 'ABC']
/Product1/Product2/Product3[#ValidYN = 'Y' and #ProductType = 'DEF']
/Product1/Product2/Product3[#ValidYN = 'Y' and #ProductType = 'GHI']
Check this other question How to parse XML using XSLT?
I need to be able to turn a flat xml data sets into html tables, and I'm having trouble finding syntax examples that will fit my need. I would like to use one stylesheet that can convert similar looking data sets into html tables with variable columns.
this is a part of my XML File :
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="XSLT_StyleSheet.xsl"?> <Services>
<Service WsdlUrl="http://venus.eas.asu.edu/WSRepository/Services/BasicThreeSvc/Service.svc">
<Name>ServiceName</Name>
<Provider></Provider>
<Category>CatName</Category>
<Operations>
<Operaion>
<Name>HelloWorld</Name>
<MsgIn>elloWorldInputMessage</MsgIn>
<MsgOut>HelloWorldOutputMessage</MsgOut>
</Operaion>
<Operaion>
<Name>OP2name</Name>
<MsgIn>InputMessage</MsgIn>
<MsgOut>OutputMessage</MsgOut>
</Operaion>
<Operaion>
<Name>Op3Name</Name>
<MsgIn>InputMessage</MsgIn>
<MsgOut>OutputMessage</MsgOut>
</Operaion>
</Operations>
this how the HTML table must look Like:
If you did not find examples of transforming XML to HTML with XSLT, then you didn't look very hard. That's one of its primary motivations. Anyway, this should get you started:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|#*"/>
<xsl:template match="/Services">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Service">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Operations">
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</xsl:template>
<xsl:template match="Operaion"> <!-- [sic] -->
<xsl:variable name="service" select="ancestor::Service"/>
<tr>
<td><xsl:value-of select="$service/Name"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="$service/Category"/></td>
</tr>
</xsl:template>
</xsl:transform>
Output on your (corrected) document (it was missing end tags):
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<tr>
<td>ServiceName</td>
<td>HelloWorld</td>
<td>CatName</td>
</tr>
<tr>
<td>ServiceName</td>
<td>OP2name</td>
<td>CatName</td>
</tr>
<tr>
<td>ServiceName</td>
<td>Op3Name</td>
<td>CatName</td>
</tr>
</tbody>
</table>
</body>
</html>