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>
Related
I have a xml file, I can access the data with xsl file. But when I add xsd to my xml file, I cannot use my xsl file. Please help me
<updateClient
xsi:schemaLocation=" file://updateClient.xsd"
xmlns="http://"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileHeader>
<messageType version="100">
<recordTp>722</recordTp>
</messageType>
<sendingCompany>some</sendingCompany>
<processDate>2018-02-13</processDate>
<recordCount>10</recordCount>
</fileHeader>
</updateClient>
My XSL file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://localhost" exclude-result-prefixes="x" >
<xsl:template match="/">
<html>
<body>
<h2>My Collection</h2>
< table>
<tr>
<xsl:for-each select="updateClient/fileHeader/messageType">
<td> <p>RecordType :<xsl:value-of select="recordTp" /></p> </td>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Your input XML is located in the http:// namespace. Your XPath expressions in the XSL file have no namespace prefix, so they are looking for elements in the default namespace.
If you can use XPath 2.0, you can use the ´ xpath-default=namespace´ attribute, i.e.:
<xsl:stylesheet version="2.0" xpath-default-namespace="http://" ...>
In XPath 1.0 you could do something like this:
<xsl:for-each select="/ns1:updateClient/ns1:fileHeader/ns1:messageType" xmlns:ns1="http://">
Or, you could remove the namespace declaration in your input XML and use xsi:noNamespaceSchemaLocation="file://updateClient.xsd" instead of xsi:schemaLocation.
Fairly new to XSL transformations, but have been searching Stack overflow for a few hours and can't quite achieve what I'm going for. I have an xml document that I want to turn in to a table via an xsl stylesheet, but I want to select only specific pars of the XML based on the values of the nodes. Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="guitarsXSLStyleSheet.xsl" type="text/xsl"?>
<guitars>
<guitar>
<model>Strat</model>
<year>1978</year>
<price>2500</price>
</guitar>
<guitar>
<model>Jaguar</model>
<year>2006</year>
<price>400</price>
</guitar>
<guitar>
<model>Strat</model>
<year>2015</year>
<price>900</price>
</guitar>
<guitar>
<model>Tele</model>
<year>1981</year>
<price>1200</price>
</guitar>
</guitars>
Now I have an xsl stylesheet, which outputs this all to a table:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<table id="guitarTable" border="1" width="200">
<tr class="header">
<th>Model</th>
<th>Year</th>
<th>Price</th>
</tr>
<xsl:apply-templates select="//guitar"/>
</table>
</xsl:template>
<xsl:template match="guitar">
<tr>
<td> <xsl:value-of select="model" /> </td>
<td> <xsl:value-of select="year" /> </td>
<td> <xsl:value-of select="price" /> </td>
</tr>
</xsl:template>
</xsl:stylesheet>
Now, let's say I'm trying to generate a table which would only show all columns for the Strats, but the Strats only. How would I do that?
I thought maybe changing the line <td> <xsl:value-of select="model" /> </td>
to
<td> <xsl:value-of select="model[text()='Strat']" /> </td>
would do it, but it still gives me a table 4 rows long, with just the non-matching model columns blanked out, but the rest still shows. How would I go about doing this? Thanks!
Change <xsl:apply-templates select="//guitar"/> to <xsl:apply-templates select="//guitar[model = 'Strat']"/>. Note that trying to learn XSLT and XPath by reading StackOverflow answer can help but any basic XPath tutorial as a starting point is probably a better way.
Sorry if my Post won´t be so good i am not that good in expressing myself.
What i want:
I´ve got an XML and a XSL Data i want to convert to an XSLT for an HTML.
As transformating i use Altova XMLSpy and this webpage: http://www.freeformatter.com/xsl-transformer.html
What i´ve got:
My XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Style.xsl"?>
<Configuration>
<Environment>
...
</Environment>
<Configurations>
...
<Config>
...
</Config>
</Configurations>
</Configuration>
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 border="4">
<xsl:apply-templates select="Configuration"/>
</table>
</body>
</html>>
</xsl:template>
<xsl:template match="Environment">
<tr>Environment</tr>
<br/>
<td> <th>Text</th>
<xsl:value-of select="Environment"/>
</td>
<td> <th>Text2</th>
<xsl:value-of select="config"/>
</td>
...
...
</xsl:template>
</xsl:stylesheet>
This is just a example how i build it.
so far everything was fine, but then:
Problem:
If i start my XML in Firefox now everything works. If i start it in IE or Chrome i just get the XML Data but not the Table.
Trying to transform it in XSLT gives me this Error:
Unable to perform XSL transformation on your XML file. null
I hope you can help me. I am a bloody beginner in XML/XSl/XSLT but i hope my post will give you the Information you need.
Greetings
Max
You have made a very common beginner's mistake:
<xsl:template match="Environment">
<tr>Environment</tr>...
<xsl:value-of select="Environment"/>
In this template, the context node is an Environment element, and your xsl:value-of instruction is looking for a child of that element with the name Environment. But your Environment element (I assume) does not have any Environment children. What you should write (I assume) is
<xsl:value-of select="."/>
However, I don't think this is the whole problem. It doesn't account for all the symptoms described. You've actually made another beginner's mistake, which is trying to run your XSLT code in the browser before testing it elsewhere. The browser-based XSLT engines give lousy diagnostics (even when you use something like Firebug). Get your code working first in a desktop environment - preferably an XML IDE like oXygen. You'll then get a decent explanation of what is wrong.
Hi Here Is The Modified Version of Your Code.
XSL Template Match Must match the XML node you wish to apply templates.
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:stylesheet version="2.0">
<xsl:template match="Configuration">
<html>
<body>
<table border="4">
<xsl:apply-templates select="Environment"/>
</table>
<table border="4">
<xsl:apply-templates select="Configurations"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Environment">
<tr>
<th>
<xsl:value-of select="name()"/>
</th>
</tr>
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
<xsl:template match="Configurations">
<tr>
<th>
<xsl:value-of select="name()"/>
</th>
</tr>
<tr>
<td>
<xsl:apply-templates select="Config"/>
</td>
</tr>
</xsl:template>
<xsl:template match="Config">
<tr>
<th>
<xsl:value-of select="name()"/>
</th>
</tr>
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Hi I have two xsl files and I have one xml how can I combine these xsl files together at transform type and combine them it and get one html
index.html
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp " "><!ENTITY bull "•">]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="hello.xsl"/>
<xsl:template match="/">
<html>
<tr>
<td><xsl:value-of select="name" /></td>
</tr>
</html>
</xsl:template>
</xsl:stylesheet>
and then my send xsl is
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp " "><!ENTITY bull "•">]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table>
<tr>
<td><xsl:value-of select="age" /></td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
and my xml is
<xml>
<name>abc</name>
<age>15</age>
</xml>
What I want output is like
<html>
<tr><td>abc</td></tr>
<table>
<tr><td>15</td></tr>
</table>
</html>
Is that is do able thing to perform in xsl I search so many site but couldn't find answer, plz help
EDIT: You edited your question and are now asking something completely different.
As I said, you can combine templates and other element from separate stylesheets with xsl:include and xsl:import. See the relevant section of the specification here.
There are ways to combine separate stylesheets (e.g. via xsl:include and xsl:import), but in your case I do not think it is even necessary.
Your stylesheets only have one template each and they simply retrieve values from an input XML which is a trivial action. There is really no need to store those two templates in separate stylesheets.
Write one stylesheet that produces both the html element and the table.
Let me emphasize another thing: It is evident in your question that you do not really understand the workings of both XSLT and HTML. To give a few hints:
the output you request is malformed HTML. A tr element cannot be an immediate child of html. Content must be placed inside body, as opposed to header.
both of the templates you show match /. Obviously, it makes no sense to combine them if they match the same node.
Please take the time to study the basics of XSLT and HTML before asking a new question.
Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" indent="yes"/>
<xsl:template match="/xml">
<html>
<body>
<table border="solid">
<tr>
<xsl:for-each select="*">
<td>
<xsl:value-of select="name()"/>
</td>
</xsl:for-each>
</tr>
<tr>
<td>
<xsl:value-of select="*[1]"/>
</td>
<td>
<xsl:value-of select="*[2]"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output
<html>
<body>
<table border="solid">
<tr>
<td>name</td>
<td>age</td>
</tr>
<tr>
<td>abc</td>
<td>15</td>
</tr>
</table>
</body>
</html>
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.