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" />
Related
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.
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
I've an XML file generated as an output of a Java program. This contains some text in the form of html in which the tags are written with < and > instead of < and > respectively. I want to convert this xml to html where in the inner html is also processed. For example:
My xml snippet:
<company>
<companyEnhancement>
Rank: -1</br> Other Links</br>http://www.gehealthcare.com/</br>
</companyEnhancement>
</company>
And, my xslt stylesheet has this part to parse it:
<td>
<xsl:value-of select="companyEnhancement"/>
</td>
But the html output on the browser, has this data as it is within a table cell:
Rank: -1 </br> Other Links</br>http://www.gehealthcare.com/</br>
I read through the links here, but I am not able to understand what exactly I should do in the stylesheet.
Please help me out. I am very new to xslt, so please excuse if it is a silly question.
Thanks
You say you have HTML in there but </br> is not HTML syntax, that would be <br> or perhaps for XHTML <br />.
If you really have escaped HTML and want to output that then you can try
<xsl:value-of select="companyEnhancement" disable-output-escaping="yes"/>
but that is only going to help if your XSLT processor serializes the result tree. Firefox for instance does not do that and does not support disable-output-escaping.
I am working on XML for first time and would highly appreciate help to solve my problem. I have tried few things but it didn't work to retrieve data from XML.
I have XML of following type
<?xml version="1.0" encoding="utf-8" ?>
<search>
<criterias>
<criteria operator="EqualTo" xmlpath="//Title/TitleKey">
<value1>1</value1>
</criteria>
<criteria operator="EqualTo" xmlpath="//Title/SubTitleKey">
<value1>1</value1>
</criteria>
<freecriteria operator="EqualTo" xmlpath="//UseMaxRowsToReturn">
<value1>1</value1>
</freecriteria>
<criteria operator="Contains" xmlpath="//Title/TitleReference" >
<value1>001</value1>
<desc>Title referenece</desc></criteria>
<criteria operator="Contains" xmlpath="//Title/TitleFamily/TitleReference">
<value1>Title number</value1>
</criteria>
</criterias>
</search>
I want retrieve value which is ' 001' which is in XML. I want put them as a hyperlink in HTML. In short I want display it in the HTML page. I have created XSL file but it didn't retrieve the value. I am trying x-query for e.g
<html>
<body>
<h1>Search</h1>
<ul>
{
for $x in doc("Titlesearch.xml")//Title/TitleReference
order by $x/TitleReference
return <li>< a href:"a.txt">{data($x/TitleReference)}. Title: {data
($x/#Title)}</a>
</li>
}
</ul>
</body>
</html>
but it is not generating any result, not even a html <h1>
I have used below xslt too.
enter code here
Application Search
Title Reference
Title Family
<td><a><xsl:value-of select="//Title/TitleReference"/></a></td>
<td><xsl:value-of select="="//Title/TitleFamily/TitleReference"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Your XPath syntax does not at all match the XML you are using as the input document. The attributes xmlpath are just strings, they do not imply any sort of XPath. The XPath's that you need should navigate the source document.
As your XPath's do not match your XML document at all, it is hard to know what you are trying to achieve exactly. However, for example, perhaps you may actually be trying to do something like the following:
<html>
<body>
<h1>Search</h1>
<ul>
{
for $x in doc("Titlesearch.xml")/search/criterias/criteria[#operator eq "Contains"][#xmlpath eq "//Title/TitleReference"]
order by $x/value1
return
<li>
Title: {$x/value1/text()}
</li>
}
</ul>
</body>
</html>
Also I would note that depending on the XQuery processor you are using, you may need to provide an absolute URI in the argument to doc, which locates your Titlesearch.xml, e.g.
doc("file:/some/path/on/your/computer/Titlesearch.xml")
You need an XSLT processor to run your XSL. Have a look at the following depending where you want to run the processor:
client-side: Saxon Client Edition ;
server-side: Saxon 9.x or Xalan.
Im working with XSLT and I'm outputting an HTML5 document. In my document I need a custom attribute.
I want to achieve this:
<div class="row" data-template>...</div>
So far I have managed to do it using CDATA, like so:
<xsl:text disable-output-escaping="yes"><![CDATA[<div class="row" data-template></div>]]></xsl:text>
Whilst this produces valid markup, the problem arises here when you need to work with the actual node, such as setting the ID dynamically. One of my cohorts suggested the following output:
<div class="row" data-template=""></div>
using:
<xsl:attribute name="data-template" />
Again this is valid, but looks somewhat ugly. Is there another method that allows me to output valid custom data attributes for HTML5?
You might want to live with the fact that <xsl:attribute> produces a name-value pair attribute but make it semantically sensible as a boolean value.
<xsl:attribute name="data-template">true</xsl:attribute>
should produce
<div class="row" data-template="true"></div>
which looks a little less ugly.
Did you already tried:
<xsl:attribute name="data-template">data-template</xsl:attribute>
However I think is not possible to get rid of key-value pairs