tags:

views:

195

answers:

2

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&lt;/uri&gt;
            </binding>
            <binding name="fname">
                <literal>Gayathri</literal>
            </binding>
            <binding name="lname">
                <literal>Vasudevan</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
------------------------
Magesh           vasudevan
Gayathri         vasudevan 

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"&gt;
    <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 Gayathri Vasudevan http://tn.gov.in/Person/43 Vivek Vasudevan http://tn.gov.in/Person/37 Magesh Vasudevan http://tn.gov.in/Person/39 Vasudevan Srinivasan

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#"&gt;
  <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&lt;/uri&gt;
      </binding>
      <binding name="fname">
        <literal>Ravi</literal>

      </binding>
      <binding name="lname">
        <literal>Kumar</literal>
      </binding>
      <binding name="title">
        <literal>Mr.</literal>
      </binding>
      <binding name="mno">

        <literal>876876</literal>
      </binding>
      <binding name="community">
        <literal>FC-Forward Caste</literal>
      </binding>
    </result>

how should i match template now?

+1  A: 

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"&gt;

    <xsl:template match="s:results">

The built-in template rules generate the output that you were seeing, since they:

  • match all templates, and perform an apply-templates
  • copy text nodes to output

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"&gt;
    <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>
Lachlan Roche
my output is like this ? http://tn.gov.in/Person/41 Gayathri Vasudevan http://tn.gov.in/Person/43 Vivek Vasudevan http://tn.gov.in/Person/37 Magesh Vasudevan http://tn.gov.in/Person/39 Vasudevan Srinivasan
Magesh
not getting any table ?
Magesh
@Magesh please edit the question and add an example of the output you want
Lachlan Roche
i tried again with yours but my output is same...!!!not able to figure out my mistake..!
Magesh
finally it worked...! thanks alot lachlan..!
Magesh
A: 

Hi,

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>

Mark Cooper
my result is same !!!!!!!!!!!!!!!! getting struck with this for past 2 days.. not able to procedd !!!http://tn.gov.in/Person/41 Gayathri Vasudevan http://tn.gov.in/Person/43 Vivek Vasudevan http://tn.gov.in/Person/37 Magesh Vasudevan http://tn.gov.in/Person/39 Vasudevan Srinivasan
Magesh