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>
Related
First attempt to code xml and use xsl for html output.
I took the W3 examples and modified them for my needs but I've missed something as looking at the xml in a browser does work correctly. None of the html code shows in the browser but the items info from the xml shows as long list.
What shows in browser:
SCALARADD Image1inSCALARADD Add one value to an image. A B A same as B allowed redLevel, greenLevel, blueLevel TRIMFILL Image1inTRIMFILL Trim an image and fill the trim area. A B A same as B allowed trimTopIndex, trimBottomIndex, trimLeftIndex, trimRightIndex fillRedLevel, fillGreenLevel, fillBlueLevel
Here is the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="routines.xsl"?>
<family>
<Image1in>
<routine>SCALARADD</routine>
<symbolic> Image1inSCALARADD </symbolic>
<operation> Add one value to an image. </operation>
<inputLayer1> A </inputLayer1>
<inputLayer2></inputLayer2>
<outputLayer1> B </outputLayer1>
<commentLayer> A same as B allowed </commentLayer>
<inputLong></inputLong>
<inputDouble> redLevel, greenLevel, blueLevel </inputDouble>
<outputLong></outputLong>
<outputDouble></outputDouble>
<comment> </comment>
</Image1in>
<Image1in>
<routine>TRIMFILL</routine>
<symbolic> Image1inTRIMFILL </symbolic>
<operation> Trim an image and fill the trim area. </operation>
<inputLayer1> A </inputLayer1>
<inputLayer2></inputLayer2>
<outputLayer1> B </outputLayer1>
<commentLayer> A same as B allowed </commentLayer>
<inputLong> trimTopIndex, trimBottomIndex, trimLeftIndex, trimRightIndex </inputLong>
<inputDouble> fillRedLevel, fillGreenLevel, fillBlueLevel </inputDouble>
<outputLong></outputLong>
<outputDouble></outputDouble>
<comment> </comment>
</Image1in>
</family>
Here is the xsl file:
<?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>Routines</h2>
<table border="2">
<tr bgcolor="#7f7f7f">
<td style="text-align:left">Title</td>
<td style="text-align:left">Symbol</td>
<td style="text-align:left">Operation</td>
<td style="text-align:left">Input Layer 1</td>
</tr>
<xsl:for-each select="family/Image1In">
<xsl:sort select="routine"/>
<tr>
<td><xsl:value-of select="routine"/></td>
<td><xsl:value-of select="symbolic"/></td>
<td><xsl:value-of select="operation"/></td>
<td><xsl:value-of select="inputLayer1"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I appreciate any help someone can share. Spent whole weekend try to fix.
Regards,
RONC
A browser is not a good environment for testing XSLT. As it happens, your XSLT has a mistake of using:
<xsl:for-each select="family/Image1In">
instead of:
<xsl:for-each select="family/Image1in">
XML is case-sensitive, and your expression selects nothing.
However, you should still be seeing the header row of the table. What you see instead - the raw text of of the XML - suggests that your browser is applying the "same origin" security rule and blocking the request for the stylesheet.
You can try this code <xsl:for-each select="family/Image1in"> insted of <xsl:for-each select="family/Image1In"> Or if you want to remove both leading and trailing space remove use normalize-space().
<?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>Routines</h2>
<table border="2">
<tr bgcolor="#7f7f7f">
<td style="text-align:left">Title</td>
<td style="text-align:left">Symbol</td>
<td style="text-align:left">Operation</td>
<td style="text-align:left">Input Layer 1</td>
</tr>
<xsl:for-each select="family/Image1in">
<xsl:sort select="routine"/>
<tr>
<td><xsl:value-of select="normalize-space(routine)"/></td>
<td><xsl:value-of select="normalize-space(symbolic)"/></td>
<td><xsl:value-of select="normalize-space(operation)"/></td>
<td><xsl:value-of select="normalize-space(inputLayer1)"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
DEMO: https://xsltfiddle.liberty-development.net/3NSSEv5
Here is an excerpt from an xsl document (inside a template technically):
<table>
<tr>
<th>INSTANCE</th>
<th>SWVER</th>
<th>SYSTEMID</th>
<th>SYSTIME</th>
<th>SYSMEM</th>
<th>CUTMEM</th>
<th>FILEMEM</th>
<th>CALCONFIG</th>
</tr>
<tr>
<td><xsl:value-of select='#INSTANCE'/></td>
<td><xsl:value-of select='#SWVER'/></td>
<td><xsl:value-of select='#SYSTEMID'/></td>
<td><xsl:value-of select='#SYSTIME'/></td>
<td><xsl:value-of select='#SYSMEM'/></td>
<td><xsl:value-of select='#CUTMEM'/></td>
<td><xsl:value-of select='#FILEMEM'/></td>
<td><xsl:value-of select="#CALCONFIG"/></td>
</tr>
</table>
Is there some way that I can avoid the redundancy of writing out the attributes both as table headers and as the attribute selection? I cannot use external sources.
I was thinking I could define some xsl variable that contains a basic structure, as follows and generate the table from there.
<list>
<item>INSTANCE</item>
...
<item>CALCONFIG</item>
</list>
The XML data is just a bunch of tags, of the same value that contain at least the above listed attributes. Each tag looks something like this:
<THING INSTANCE="boop" SWVER="foo" SYSTEMID="123"
...
CALCONFIG="cal.cfg" SOMETHINGELSE="bar"
/>
To illustrate the point I made in a comment to your question, consider the following example:
XML
<root>
<item color="red" name="alpha" size="small" garbage="123" id="1"/>
<item color="green" name="bravo" size="medium" garbage="456" id="2"/>
<item color="blue" name="charlie" size="large" garbage="789" id="3"/>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<xsl:output method="html" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<my:columns>
<col>id</col>
<col>name</col>
<col>size</col>
<col>color</col>
</my:columns>
<xsl:variable name="columns" select="document('')/xsl:stylesheet/my:columns" />
<xsl:template match="/root">
<table border="1">
<tr>
<xsl:for-each select="$columns/col">
<th>
<xsl:value-of select="." />
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="item">
<xsl:variable name="attributes" select="#*" />
<tr>
<xsl:for-each select="$columns/col">
<td>
<xsl:value-of select="$attributes[name()=current()]" />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Result (rendered)
Bryant, you need to work with two files (XML and XSL):
First, you need to specify a template (example.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>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>instance</th>
<th>swver</th>
<th>systemid</th>
<th>systime</th>
<th>sysmem</th>
<th>cutmem</th>
<th>filemem</th>
<th>calconfig</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="instance"/></td>
<td><xsl:value-of select="swver"/></td>
<td><xsl:value-of select="systemid"/></td>
<td><xsl:value-of select="systime"/></td>
<td><xsl:value-of select="sysmem"/></td>
<td><xsl:value-of select="cutmem"/></td>
<td><xsl:value-of select="filemem"/></td>
<td><xsl:value-of select="calconfig"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
And then you need to define your XML file (example.xml), based on the template (example.xsl):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<catalog>
<cd>
<instance>instance1</instance>
<swver>swver1</swver>
<systemid>systemid1</systemid>
<sysmem>sysmem1</sysmem>
<systime>systime1</systime>
<cutmem>cutmem1</cutmem>
<filemem>filemem1</filemem>
<calconfig>calconfig1</calconfig>
</cd>
<cd>
<instance>instance2</instance>
<swver>swver2</swver>
<systemid>systemid2</systemid>
<sysmem>sysmem2</sysmem>
<systime>systime2</systime>
<cutmem>cutmem2</cutmem>
<filemem>filemem2</filemem>
<calconfig>calconfig2</calconfig>
</cd>
.
.
</catalog>
Result:
Reference:
http://www.w3schools.com/xsl/xsl_transformation.asp
Hope this helps!
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>
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>
I'm learning XML and I'm running into the following problem: I have an attribute in my XML file which can have a color as content (e.g. <color>red</color>), but I don't know how to use it.
My XML:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car year="2002" manufacturer="Jet" model="Sardine Can 1.6L">
<meter>95664</meter>
<color>silver</color>
<price>099900</price>
<dealersecurity buyback="no"/>
</car>
<car year="2004" manufacturer="Jet" model="Sardine Can 2.0">
<meter>81283</meter>
<color>red</color>
<price>129900</price>
<dealersecurity buyback="no"/>
</car>
<car year="2007" manufacturer="Jet" model="Sardine Can 2.0">
<meter>49741</meter>
<color>black</color>
<price>169999</price>
<dealersecurity buyback="yes"/>
</car>
</cars>
My XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/cars">
<html>
<head><title>Second Hand Sardine Cans</title>
<link rel="stylesheet" type="text/css" href="style.css"/></head>
<body>
<h1>Second Hand Sardine Cans</h1>
<table border="1"><th>Make</th><th>Model</th><th>Year</th><th>KMs</th><th>Color</th><th>Price</th><th>Warranty</th><xsl:apply-templates/></table>
</body>
</html>
</xsl:template>
<xsl:template match="cars/car">
<tr><xsl:for-each select="cars/car"/>
<td><xsl:value-of select="#manufacturer" /></td>
<td><xsl:value-of select="#model" /></td>
<td><xsl:value-of select="#year" /></td>
<td><xsl:value-of select="meter" /></td>
<xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="meter"/>
<xsl:template match="color">
<td><xsl:apply-templates /></td>
</xsl:template>
<xsl:template match="price">
<td><xsl:apply-templates /></td>
</xsl:template>
</xsl:stylesheet>
What I have until now:
But instead of it saying "Red" or "Black", I want that cell to fill with that exact color.
Thanks!
Edit: I fixed it with the help of Dimitre's answer. I added <td bgcolor="{color}"><xsl:value-of select="color" /></td> below <td><xsl:value-of select="meter" /></td>
Just change in your transformation:
<tr>
to:
<tr bgcolor="{color}">
Or, if you really want to have just one cell with that color, use in the template that matches color:
<td bgcolor="{.}">