Generation of XSLT From XML - html

I have an XML File with very large content.
I had an xsd file for that xml file.
For example : Please visit this link
XML CONTENT : http://formalmind.com/sites/default/files/blog/manual-testing.reqif
XSD For that XML : https://www.omg.org/spec/ReqIF/20110401/reqif.xsd
I need to display that xml in HTML page. For that i need XSLT.
Is it possible to generate XSLT for the given XML Automatically ? or is there is any other way to display in html Page?
Html Result Should be like
HTML_OUTPUT_IMAGE
Thanks
Sivabalakrishnan

Yes, this is possible, but it's not clear that it's a useful approach. What information in the schema do you intend to use to generate your XSLT? You need to give some examples of constructs that you find in the schema, and the resulting XSLT rules that you want to generate.
There are two other options you should consider:
(a) writing a completely generic XSLT stylesheet, that handles any document regardless of its schema. For example, it could contain rules like this:
<xsl:template match="*[count(*)>1 and count(*)=count(distinct-values(*/node-name())]">
<table>
<xsl:for-each select="*">
<tr>
<td><xsl:value-of select="name()"/></td>
<td><xsl:apply-templates/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
This rule says that if you encounter an element with 2 or more element children, and all the children have distinct names, then display a table containing the element names in one column and their values in another.
(b) writing a generic schema-aware (XSLT 2.0) stylesheet that handles any input document, but uses schema information from the type annotations on validated nodes to decide what output to generate. For example you might have a rule like this:
<xsl:template match="#*[data(.) instance of xs:decimal]">
<xsl:value-of select="format-number(., '#,00', 'continental')"/>
</xsl:template>
which causes all decimal attribute values to be displayed using a format such as 3,14.

Related

How to access elements of a subelement using XSLT?

I have the following XML code:
http://codeshare.io/MQyQu
and am using the following XSLT code:
http://codeshare.io/LWl6g
in coordination with the W3 Schools "Tryit Editor" tool for XSLT. I can't link it due to reputation.
My problem is that the XSLT does not parse all the elements (semester elements within a catalog element, and class elements within a semester element).
The W3 schools example of XSLT uses only one subelement, and I'm at a loss for what to do for two subelements.
My understanding is that the first
<xsl:for-each select="catalog/semester">
will loop semester elements in the catalog elements.
Within the loop, I have a HTML table where I want to output the information of the class element, that is a subelement of semester. I think nesting this code in the original for-each loop should output the text for each element for each class in the table:
<xsl:for-each select="class">
<td><xsl:value-of select="dept"/></td>
<td><xsl:value-of select="number"/></td>
<td><xsl:value-of select="title"/></td>
</xsl:for-each>
In the HTML table, only the text, "CIT", of the first subelement, dept, of the element class is displayed.
I deduct that the for-each loop isn't parsing. If that is the reason, why?
If my deduction on why the XSLT code isn't reading the XML code how I want it is incorrect, what is the correct reason and how can I fix it?
There are several problems here...
Your input XML isn't well formed - there are tags that begin <number> but aren't closed (it seems like the tag that ends with </dept> is meant to close it).
Your input XML contains unescaped ampersands. Replace & with &
Your Stylesheet is invalid because it's missing a </table> tag. You need to add it in.
Your inner for-each loop should probably have <tr> and </tr> tags around the <td>...</td> nodes.
Here's an XSLTransform with all of these problems addressed (and I added an output instruction so that XSLTransform can show it as HTML in the window):
http://xsltransform.net/94rmq66
So the bottom line is that you're right, the XSLT parser can't read the XML because it's malformed XML. The fact that it's doing anything at all with your input is pretty bad, and another reason not to use the W3Schools XSLT tools.

Styling inline XML tags with XSLT

This is a similar question to Style inline text along with nested tags with XSLT, but I can't comment to get clarification, so I will elaborate my specific scenario here. I basically have an XML document with the following structure:
<book>
<chapter>
<para>This is some text about <place>New York</place></para>
</chapter>
</book>
I am using XSLT to output XHTML from my XML file, and I want to be able to put span tags or something around the content in the place tag in the example above. The purpose is so that I can style these segments of text with CSS. Following the example I referenced above, I added this:
<xsl:template match="book/chapter/para/place">
<span class="place">
<xsl:apply-templates/>
</span>
</xsl:template>
When I load the XML document in the browser I get the error: "Error loading stylesheet: Parsing an XSLT stylesheet failed." (the stylesheet was loading properly before I added this part)
I'm assuming I lack some basic understanding of how xsl:apply-templates should be used. I would appreciate it if someone could point me in the direction of figuring this out.
Thanks!
The match:
<xsl:template match="book/chapter/para/">
applies templates to all children of the place element, rather than place itself.
Use select within apply-templates instead:
<xsl:template match="/">
<xsl:apply-templates select="book/chapter/para/place"/>
</xsl:template>
In the absence of a select attribute, the xsl:apply-templates instruction processes all of the children of the current node, including text nodes.
A select attribute can be used to process nodes selected by an expression instead of processing all children. The value of the select attribute is an expression. The expression must evaluate to a node-set.
References
XSLT 1.0 Specification

XSL applying tags incorrectly

I have a class project I'm working on but I've hit a point where I'm stuck and don't know what to do. My XSL (style6.xsl) works to an extent but it creates text instead of wrapping a chunk of my xml (Project6style.xml) so I have 2 sets of text but one isn't wrapped in any tags. I've spent all day trying to figure out why it's been doing this but I can't figure it out. I was hoping someone would be able to take a look at it.
If anyone could help it would be appreciated!
https://www.mediafire.com/?4b74nb1iltdqsqx
File Reference:
style6.xsl (what I need to edit so it looks a certain way)
Project6style.xml (what was given to me in the class project folder)
Tutorial.06 Project.doc (The assignment word document)
XSL Current Output.html (what my current xsl file is giving me)
XSL Output - WHAT IT SHOULD BE.html (What I'm assuming my professor wants it to look like)
Code for those who don't want to download:
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0"/>
<xsl:template match="project">
<html>
<body>
<xsl:apply-templates/>
<h1>
<xsl:value-of select="student"/>
</h1>
<h2>
<xsl:value-of select="date"/>
</h2>
</body>
</html>
</xsl:template>
<xsl:template match="objective">
<xsl:apply-templates/>
<h4>
<xsl:value-of select=".//name"/>
</h4>
<div>
<xsl:value-of select="description"/>
</div>
</xsl:template>
</xsl:stylesheet>
XML:
<?xml version="1.0"?>
<!-- XML Project 6 -->
<?xml-stylesheet type="text/xsl" href="style6.xsl" ?>
<project>
<student>Student</student>
<date>Date</date>
<objective>
<name>Working with XSL</name>
<description>XSL is composed of three parts: XSL-FO
(Extensible Style sheet Language - Formatting Objects),
XSLT (Extensible Style sheet Language Transformations),
and XPath. XSL-FO is used to implement page layout and design.
XSLT is used to transform XML content into another presentation format.
XPath is used to locate information from an XML document
and perform operations and calculations upon that content.
</description>
</objective>
<objective>
<name>Introducing XSLT style sheets and processors</name>
<description>An XSLT style sheet contains instructions for transforming
the contents of an XML document into another format. An XSLT style
sheet document is itself an XML document, but has an extension .xsl.
An XSLT style sheet converts a source document of XML content into
a result document containing the markup codes and other instructions
for formatting.
</description>
</objective>
<objective>
<name>Creating an XSLT style sheet</name>
<description>To attach an XML file to the style sheet,
insert the processing instruction following the first line
in the document. An XSLT style sheet has the general structure
of all XML documents.
</description>
</objective>
</project>
HTML result (What my HTML file displays with these 2 files in their current state):
Student
Date
Working with XSL
XSL is composed of three parts: XSL-FO
(Extensible Style sheet Language - Formatting Objects),
XSLT (Extensible Style sheet Language Transformations),
and XPath. XSL-FO is used to implement page layout and design.
XSLT is used to transform XML content into another presentation format.
XPath is used to locate information from an XML document
and perform operations and calculations upon that content.
<h4>Working with XSL</h4><div>XSL is composed of three parts: XSL-FO
(Extensible Style sheet Language - Formatting Objects),
XSLT (Extensible Style sheet Language Transformations),
and XPath. XSL-FO is used to implement page layout and design.
XSLT is used to transform XML content into another presentation format.
XPath is used to locate information from an XML document
and perform operations and calculations upon that content.
</div>
Introducing XSLT style sheets and processors
An XSLT style sheet contains instructions for transforming
the contents of an XML document into another format. An XSLT style
sheet document is itself an XML document, but has an extension .xsl.
An XSLT style sheet converts a source document of XML content into
a result document containing the markup codes and other instructions
for formatting.
<h4>Introducing XSLT style sheets and processors</h4><div>An XSLT style sheet contains instructions for transforming
the contents of an XML document into another format. An XSLT style
sheet document is itself an XML document, but has an extension .xsl.
An XSLT style sheet converts a source document of XML content into
a result document containing the markup codes and other instructions
for formatting.
</div>
Creating an XSLT style sheet
To attach an XML file to the style sheet,
insert the processing instruction following the first line
in the document. An XSLT style sheet has the general structure
of all XML documents.
<h4>Creating an XSLT style sheet</h4><div>To attach an XML file to the style sheet,
insert the processing instruction following the first line
in the document. An XSLT style sheet has the general structure
of all XML documents.
</div>
<h1>Student</h1><h2>Date</h2>
This is one possible stylesheet, merely sticking to the structure of the input XML document:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" />
<xsl:template match="objective">
<h4><xsl:value-of select="name" /></h4>
<div><xsl:value-of select="description" /></div>
</xsl:template>
<xsl:template match="project">
<h1><xsl:value-of select="student" /></h1>
<h2><xsl:value-of select="date" /></h2>
<xsl:apply-templates select="objective" />
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="project" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
note: I like to be specific about the target(s) of my apply-templates elements (hence the select="..." attribute), while some people prefer implicit selection; the choice is entirely up to you.

XSL Processing Order

I've had trouble finding an exact, and simple, answer to this question on SO or elsewhere:
In XSL files, how can you tell which template will be processed first, second, etc? I read that it was ordered by how specific the XPath was. Additionally, is there a difference in XSL 1.0 vs. 2.0?
Finally, here is a flawed XSL file I am toying with. Currently the output is just the title "Table of Contents". I'll attach the XML here as well.
<xsl:template match="/">
<h1>
<xsl:text>Table of Contents</xsl:text>
</h1>
</xsl:template>
<xsl:template match="heading1">
<h2>
<xsl:value-of select="."/>
</h2>
</xsl:template>
<p>
<xsl:text>This document contains </xsl:text>
<xsl:value-of select="count(/article/body/heading1)"/>
<xsl:text> chapters. </xsl:text>
</p>
and the XML:
<article>
<title>
Creating output
</title>
<body>
<heading1>Generating text</heading1>
<heading1>Numbering things</heading1>
<heading1>Formatting numbers</heading1>
<heading1>Copying nodes from the input document to the output</heading1>
<heading1>Handling whitespace</heading1>
</body>
Any explanation as to why all the content isn't being displayed? Thank you for your help!
Here's what's happening:
The XSLT processor reads the root element of your XML
Then it looks in the stylesheet to see what matches. It finds your first template
It executes the first template.
The first template says to output the text, and then does nothing else, so the XSLT processor moves on to the next input element.... but you've processed the entire root node, so there are no more input nodes at the same level. It's done.
What you need to do is put an <xsl:apply-templates/> inside the first template. When this is encountered by the processor, it starts over but this time the context is the list of second-level nodes inside the root. It will look at each XML node in turn, find the best matching template in your stylesheet, and execute it.
This is a key concept -- The template is NOT in control, and is not procedural.

Import an HTML table using XSL

I have constructed an XSL file that parses an XML formatted log and generates an HTML page with information from the log. In this same XSL file, I am attempting to use XSL to pull in an external HTML file that contains a table that I want to display. As it stands, I can display the entire page that contains the table, but am unable to isolate the table. How can this be done? Currently, I can pull in the entire page using the following code:
<xsl:copy-of select="document($tablePageUrl)" />
However, I don't understand how to traverse the HTML body and pull out a copy of the table. An example of how the HTML document is formatted (the document is proper XML, but not proper HTML):
<html>
<head>
</head>
<body>
<table>
Table Contents
</table>
</body>
</html>
Sounds like you want to use XPath to select a particular element in the document.
Try something like:
<xsl:copy-of select="document($tablePageUrl)/html/body/table" />
From comments:
I had tried using XPath but was using
incorrect syntax
From http://www.w3.org/TR/xpath/#node-sets
The / and // operators compose an
expression and a relative location
path. It is an error if the expression
does not evaluate to a node-set. The
/ operator does composition in the
same way as when / is used in a
location path
In particular, this syntax:
document($tablePageUrl)/html/body/table
Following the production:
PathExpr ::= FilterExpr '/' RelativeLocationPath
FilterExpr ::= PrimaryExpr
PrimaryExpr ::= FunctionCall
Check to see whether the HTML is in a namespace. (Look for a telltale xmlns="....".) Your sample isn't in a namespace, but from experience, if people don't realise that namespaces matter, they often remove the namespace declaration when posting samples. If the elements are in a namespace, then the XPath expression needs to use prefixed names to select them.
<xsl:variable name="source-html" select="document('url')" />
<xsl:value-of select="$source-html//table" />