For some reason, colons in the tags of my XML files seem to be causing problems when I'm trying to render it in HTML format using XSL. I think it's the im: portion of the tag name, so are special characters just not allowed? Below is my xml.
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type="text/xsl" href="formatting.xsl" xmlns:im="im"?>
<im:Command xmlns:im="im">
<im:CommandHdr>
<im:Name>John</im:Name>
<im:DisplayName>Jo</im:DisplayName>
</im:CommandHdr>
</im:Command>
Here is my 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="/">
<html>
<body>
<table>
<xsl:for-each select="im:Command/im:CommandHdr">
<tr>
<td><xsl:value-of select="im:Name"/></td>
<td><xsl:value-of select="im:DisplayName"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How do I fix this problem.
Related
I'm getting this error when trying to convert a XML document with XSL. I've only just started coding with XML, so I am sure I am missing something simple, but cant seem to find much information about it online.
Booklist.XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="booklist.xsl"?>
<CATALOG>
<BOOK>
<NAME>
<TITLE>Caring for Sheep</TITLE>
</NAME>
<PAGES> 60 </PAGES>
<PRICE>$34.99</PRICE>
</BOOK>
</CATALOG>
And my XSL File:
Booklist.XSL
<?xml version="1.0"?>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name">;
</div>
</xsl:for-each>
<xsl:for-each select="CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
Can anyone see an error here?
You're sort of mixing the Literal Result Element as Stylesheet simplified syntax with the regular syntax by putting a literal result element <body> outside any template, yet not at the top level. If you want to use this simplified syntax, get rid of the <xsl:stylesheet> and <xsl:output> elements, so that <body> is at the top level.
Also, XML and XPath are case-sensitive with regard to element names, so you'll need to fix the inconsistency between e.g. "catalog" and <CATALOG>.
Well, if the for some reasons which are hard to figure out the simplified syntax doesn't work for you I would suggest using the "normal coding", which would be something like this in your case:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:template match="/">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="/CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name"/>
</div>
</xsl:for-each>
<xsl:for-each select="/CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
I am attempting to write an XSLT for an XML file I have and I am getting no results.
A simple example of my XML:
<Monsters>
<Monster>
<Name>Dracula</Name>
<Actor>Bela Lugosi</Actor>
</Monster>
</Monsters>
And a simple XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body style="background: #FFFFCC">
<xsl:for-each select="Monsters/Monster">
<xsl:value-of select="Name" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl>
The resulting display is entirely empty and I just know it's something silly that I'm missing.
You don't have a proper closing tag for xsl:stylesheet element.
I am trying to do something possibly really simple, but I just dont understand this as to why its not working from what I have read up on.
Example XML:
<example>
<item>
<name>something1</name>
<qty>1</qty>
</item>
<item>
<name>something2</name>
<qty>1</qty>
</item>
<item>
<name>something3</name>
<qty>2</qty>
</item>
</report>
Sample XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" />
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="297mm" page-width="210mm">
<fo:region-body margin-top="20mm" margin-bottom="20mm"/>
<fo:region-before extent="10mm"/>
<fo:region-after extent="10mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4" >
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="report/item"/>
</fo:flow>
</fo:page-sequence>
<fo:page-sequence master-reference="A4" >
<fo:flow flow-name="xsl-region-before">
Some head content.
</fo:flow>
</fo:page-sequence>
<fo:page-sequence master-reference="A4" >
<fo:flow flow-name="xsl-region-after">
Some foot content.
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="item">
<fo:block><xsl:value-of select="qty" />, <xsl:value-of select="name" /></fo:block>
</xsl:template>
</xsl:stylesheet>
Desired Result:
I want to be able to do a transformation that appears in a browser, but creates a page with common Header and Footer that always occurs, and the information that fills up the page until it runs out of room, then starts another with the same Head and Foot information.
ie - if there was LOTS of Items, and I got the and of each one on a line until it ran out of A4 space, then continued on another A4 page.
It is not important to have to see the page layout in the browser, but do need to see it layout that way in the Printing.
Have I missed something here? or am I on the right track?
also suggestions of good sources on learning XSL-FO would be appreciated, as most I have seen are hard to follow with this idea, w3schools does a tutorial, but it describes it as stand alone, without need for and XSLT.
Added (9th Apr 2014)
Sample Raw XSL instead of XSL-FO
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" />
<xsl:template match="/">
<h3>Some Form</h3>
<table>
<tr>
<th>SomeName</th>
<th>Qty</th>
</tr>
<xsl:apply-templates select="Report/Item"/>
</table>
<h4>Some footer</h4>
</xsl:template>
<xsl:template match="item">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Here is my xml:
<Catalog>
<catalogDetail catalogId="DemoCatalog">
<catalogName>Demo Catalog</catalogName>
</catalogDetail>
<catalogDetail catalogId="GoogleCatalog">
<catalogName>Google Catalog</catalogName>
</catalogDetail>
</Catalog>
I want it to be read in HTML file how can I do this???
To do this your HTML file should contain some JavaScript code, so you will want to learn how to parse XML in Javascript.
Here is a good StackOverflow question on this topic: XML parsing in JavaScript
You can do by using PHP's XML Library called simplexml for more information check this link
http://www.w3schools.com/php/php_xml_simplexml.asp
NOTE : If you can elaborate on what technology you're using, I'll try to provide a more complete example.
I would suggest using XSLT for this. With XSLT, you can pass in the XML fragment, parse it, and return formatted HTML.
Here's an example XSLT document that converts XML to HTML:
<?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="html" indent="yes"/>
<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>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I want to change this xml content to HTML table
<SSI>
<data>
<expanded>Chemical Research</expanded><abbre>Chem. Res.</abbre>
<expanded>Materials Journal</expanded><abbre>Mater. J.</abbre>
<expanded>Chemical Biology</expanded><abbre>Chem. Biol.</abbre>
<expanded>Symposium Series</expanded><abbre>Symp. Ser.</abbre>
<expanded>Biochimica Polonica</expanded><abbre>Biochim. Pol.</abbre>
<expanded>Chemica Scandinavica</expanded><abbre>Chem. Scand.</abbre>
<\data>
<data>
<expanded>Botany</expanded><abbre>Bot.</abbre>
<expanded>Chemical Engineering</expanded><abbre>Chem. Eng.</abbre>
<expanded>Chemistry</expanded><abbre>Chem.</abbre>
<expanded>Earth Sciences</expanded><abbre>Earth Sci.</abbre>
<expanded>Microbiology</expanded><abbre>Microbiol.</abbre>
<\data>
<\SSI>
Tried with following XSL
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Abbreviate</title></head>
<body>
<table border="1">
<tr>
<th>Expanded</th>
<th>Abbre</th>
</tr>
<xsl:for-each select="SSI/data">
<tr>
<td><xsl:value-of select="expanded"/></td>
<td><xsl:value-of select="abbre"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
I got only the first entry of data tag in HTML Table format
Expanded Abbre
----------- --------------------
Chemical Research Chem. Res
Botany Bot.
how can get all the values in HTMl???
If you clean up your XSLT and use xsl:apply-templates rather than xsl:for-each, life will become simpler. There is almost never a reason to use xsl:for-each. Try this:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Abbreviate</title></head>
<body>
<table border="1">
<tr>
<th>Expanded</th>
<th>Abbre</th>
</tr>
<xsl:apply-templates select='SSI/data/expanded'/>
</table>
</body></html>
</xsl:template>
<xsl:template match="expanded">
<tr>
<td><xsl:apply-templates/></td>
<xsl:apply-templates select='following-sibling::abbre[1]'/>
</tr>
</xsl:template>
<xsl:template match="abbre">
<td><xsl:apply-templates/></td>
</xsl:template>
</xsl:stylesheet>
By using small templates that are applied, you simplify your stylesheet. Additionally, there is no real reason to use xsl:value-of here - the built-in templates will do the right thing. You will end up with simpler templates that are easier to understand.