Reputation:
I need to generate an xsl table for the xml below, for attributes fname and lname. I guess I have done something wrong in xpath. Could someone help me out by writing an xsl table for the xml below..
<sparql>
<head>
<variable name="s"/>
<variable name="fname"/>
<variable name="lname"/>
</head>
<results>
<result>
<binding name="s">
<uri>http://tn.gov.in/Person/41</uri>
</binding>
<binding name="fname">
<literal>G</literal>
</binding>
<binding name="lname">
<literal>Vn</literal>
</binding>
</result>
<!-- more result elements -->
</results>
</sparql>
like i have an servlet which queries a semantic data , using jena... the output of the servlet is above xml... while setting the output format Jena has an option in which the XML can be styled mapping the xsl file..
now when i used lachlan's example i got output as i posted in that comment.. nothing , my output must be in the form of an Table in which fname,lname should be displayed
like
fname lname
------------------------
M v
G v
etc...
what's the mistake i must have done ? this is my xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="sparql/results">
<html>
<head><title>persons</title>
</head>
<body>
<table width="40%" border="1">
<THEAD>
<TR>
<TD><B>first name</B></TD>
<TD><B>last name</B></TD>
</TR>
</THEAD>
<TBODY>
<xsl:for-each select="result">
<TR>
<TD><xsl:value-of select="binding[@name='fname']/literal/text()" /></TD>
<TD><xsl:value-of select="binding[@name='fname']/literal/text()" /></TD>
</TR>
</xsl:for-each>
</TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
My output is:
http://tn.gov.in/Person/41 Gn http://tn.gov.in/Person/43 Vn http://tn.gov.in/Person/37 Mn http://tn.gov.in/Person/39 Vn
i dint put the name='s' in the xsl def.. but i am getting that also in the output that too wihtout formatting as table..
YES i have nampespace for the root sparql..
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="s"/>
<variable name="fname"/>
<variable name="lname"/>
<variable name="title"/>
<variable name="mno"/>
<variable name="community"/>
</head>
<results>
<result>
<binding name="s">
<uri>http://tn.gov.in/Person/45</uri>
</binding>
<binding name="fname">
<literal>/literal>
</binding>
<binding name="lname">
<literal>K</literal>
</binding>
<binding name="title">
<literal>Mr.</literal>
</binding>
<binding name="mno">
<literal>876876</literal>
</binding>
<binding name="community">
<literal>Fe</literal>
</binding>
</result>
how should i match template now?
Upvotes: 0
Views: 2012
Reputation: 25946
Your stylesheet has only has a single template rule: match="sparql/results"
which should be matched. Does your input document contain namespaces which are not shown in the example?
If your xml elements are in a namespace, even if it is the default namespace for the document, you must use namespace prefixes in any XPath expressions and template match rules. It is the namespace uri and not the prefix that matters. Note that attributes will not be in the default namespace, they only have a namespace if their name has a prefix.
<xsl:stylesheet version="1.0"
xmlns:s="http://www.w3.org/2005/sparql-results#"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="s:results">
The built-in template rules generate the output that you were seeing, since they:
I have added a rule to match /
, and then explicitly select the desired elements at each step.
This example produces a HTML table from your input, containing columns for first name and last name.
<xsl:stylesheet version="1.0"
xmlns:s="http://www.w3.org/2005/sparql-results#"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>persons</title>
</head>
<body>
<xsl:apply-templates select="s:sparql/s:results" />
</body>
</html>
</xsl:template>
<xsl:template match="s:results">
<table>
<thead>
<tr>
<td>
<xsl:text>Link</xsl:text>
</td>
<td>
<xsl:text>First name</xsl:text>
</td>
<td>
<xsl:text>Last name</xsl:text>
</td>
</tr>
</thead>
<tbody>
<xsl:apply-templates />
</tbody>
</table>
</xsl:template>
<xsl:template match="s:result">
<tr>
<td>
<a href="{s:binding[@name='s']/s:uri}">
<xsl:value-of select="s:binding[@name='s']/s:uri" />
</a>
</td>
<td>
<xsl:value-of select="s:binding[@name='fname']/s:literal" />
</td>
<td>
<xsl:value-of select="s:binding[@name='lname']/s:literal" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation:
I didn't try Lachlan's answer, but it looks good to me. I put this together quickly that gives the output you asked for (it's not too different, and I haven't done any formatting).
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:apply-templates></xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="results">
<table>
<tr>
<th>Fname</th>
<th>Lname</th>
</tr>
<xsl:for-each select="result">
<tr>
<td><xsl:value-of select="binding[@name='fname']"/></td>
<td><xsl:value-of select="binding[@name='lname']"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
Upvotes: 1