retrieve data from xml and display it in html - html

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.

Related

Generation of XSLT From XML

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.

HTML within XML is not displayed properly in the output after conversion using XSLT

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.

XSLT strange behavior with umlaut (mutation)

I have a very strange behavior when I got mutations in a node in xml when I try to parse it with an XSLT.
So we now assume that CustomerName has the Value 'MÖP'.
In this case, the resulting HTML 'a' tag would have a href to 'M%C3%B6P'
<a href="{CustomerName}">
<xsl:value-of disable-output-escaping="yes" select="CustomerName"/>
</a>
In this case, the resulting HTML 'div' tag would have an id 'MÖP'
<div style="display:none">
<xsl:attribute name="id"><xsl:value-of select="CustomerName"/></xsl:attribute>
</div>
It seems like the attribute href in the a tag has something to do with it.
My question is, why is it like that ?
An what can i do that in both cases i got the same output ?
Greetz
The XSLT processor is doing you a favour by escaping the URL value in the href attribute of the HTML link element. This is by design, see http://www.w3.org/TR/xslt-xquery-serialization/#HTML_ESCAPE-URI-ATTRIBUTES. If you really don't want that and you use an XSLT 2.0 processor then you can use <xsl:ouput method="html" escape-uri-attributes="no"/> in your stylesheet.

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" />

Is there a way to get YQL to return HTML?

I am trying to use YQL to extract a portion of HTML from a series of web pages. The pages themselves have slightly different structure (so a Yahoo Pipes "Fetch Page" with its "Cut content" feature does not work well) but the fragment I am interested in always has the same class attribute.
If I have an HTML page like this:
<html>
<body>
<div class="foo">
<p>Wolf</p>
<ul>
<li>Dog</li>
<li>Cat</li>
</ul>
</div>
</body>
</html>
and use a YQL expression like this:
SELECT * FROM html
WHERE url="http://example.com/containing-the-fragment-above"
AND xpath="//div[#class='foo']"
what I get back are the (apparently unordered?) DOM elements, where what I want is the HTML content itself. I've tried SELECT content as well, but that only selects textual content. I want HTML. Is this possible?
You could write a little Open Data Table to send out a normal YQL html table query and stringify the result. Something like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<sampleQuery>select * from {table} where url="http://finance.yahoo.com/q?s=yhoo" and xpath='//div[#id="yfi_headlines"]/div[2]/ul/li/a'</sampleQuery>
<description>Retrieve HTML document fragments</description>
<author>Peter Cowburn</author>
</meta>
<bindings>
<select itemPath="result.html" produces="JSON">
<inputs>
<key id="url" type="xs:string" paramType="variable" required="true"/>
<key id="xpath" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute><![CDATA[
var results = y.query("select * from html where url=#url and xpath=#xpath", {url:url, xpath:xpath}).results.*;
var html_strings = [];
for each (var item in results) html_strings.push(item.toXMLString());
response.object = {html: html_strings};
]]></execute>
</select>
</bindings>
</table>
You could then query against that custom table with a YQL query like:
use "http://url.to/your/datatable.xml" as html.tostring;
select * from html.tostring where
url="http://finance.yahoo.com/q?s=yhoo"
and xpath='//div[#id="yfi_headlines"]/div[2]/ul/li'
Edit: Just realised this is a pretty old question that was bumped; at least an answer is here, eventually, for anyone stumbling on the question. :)
I had this same exact problem. The only way I have gotten around it is to avoid YQL and just use regular expressions to match the start and end tags :/. Not the best solution, but if the html is relatively unchanging, and the pattern just from say <div class='name'> to <div class='just_after>`, then you can get away with that. Then you can get the html between.
YQL converts the page into XML, then does your XPath on it, then takes the DOMNodeList and serializes that back to XML for your output (and then converts to JSON if needed). You can't access the original data.
Why can't you deal with XML instead of HTML?