Using XSL transformation for XML to HTML conversio

2019-09-03 04:30发布

问题:

I have written an XSL file for displaying an XML file in HTML tabular format, but it's not working. It's only showing the headers, i.e. uri and literal. Kindly go through my code and reply to me with solution.

<?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="/">
    <html>
      <head>
        <title>Query</title>
      </head>
      <body>
        <table width="100%" border="1">
          <THEAD>
            <TR>
              <TD width="35%">
                <B>URI</B>
              </TD>
              <TD width="15%">
                <B>Literal</B>
              </TD>
            </TR>
          </THEAD>
          <TBODY>
            <xsl:for-each select="sparql/results/result">
              <TR>
                <TD width="35%">
                  <xsl:value-of select="uri" />
                </TD>
                <TD width="15%">
                  <xsl:value-of select="literal" />
                </TD>
              </TR>
            </xsl:for-each>
          </TBODY>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

My input is:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="c1"/>
    <variable name="callret-1"/>
  </head>
  <results>
    <result>
      <binding name="c1">
        <uri>http://dbpedia.org/resource/SVIST</uri>
      </binding>
      <binding name="callret-1">
        <literal>
          Swami Vivekananda &lt;b&gt;Institute&lt;/b&gt; &lt;b&gt;of&lt;/b&gt; Science and      &lt;b&gt;Technology&lt;/b&gt;, a non profit making trust has been set up to... the technological and professional institution &lt;b&gt;of&lt;/b&gt; high standards and to encourage research and... In this era &lt;b&gt;of&lt;/b&gt; economic...
        </literal>
      </binding>
    </result>
    <result>
      <binding name="c1">
        <uri>http://dbpedia.org/resource/Haldia_Institute_of_Technology</uri>
      </binding>
      <binding name="callret-1">
        <literal>
          &lt;b&gt;Haldia&lt;/b&gt; &lt;b&gt;Institute&lt;/b&gt; &lt;b&gt;of&lt;/b&gt; &lt;b&gt;Technology&lt;/b&gt;.
        </literal>
      </binding>
    </result>
  </results>
</sparql>

回答1:

You need to use XML Namespaces in your stylesheet to match your input document.

Your input document uses a default namespace of

http://www.w3.org/2005/sparql-results#

Selectors that do not explicitly reference this namespace (with a prefix) will not work.

You need to add a namespace to your stylesheet like this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:spa="http://www.w3.org/2005/sparql-results#">

(Note that you can use whatever prefix you want.)

Then modify your XPath selectors to use this prefix:

<xsl:for-each select="spa:sparql/spa:results/spa:result">
    <TR>    
       <TD width="35%"><xsl:value-of select="spa:uri" /></TD>   
       <TD width="15%"><xsl:value-of select="spa:literal" /></TD> 
    </TR>
</xsl:for-each>

Also, use indentation and note that HTML elements are lowercase.



回答2:

Harpo was right, you just need to include namespace within your XSLT and change your XPATH accordingly. I have done this for you:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:sq="http://www.w3.org/2005/sparql-results#">
  <xsl:output method="html" encoding="UTF-8"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Query</title>
      </head>
      <body>
        <table width="100%" border="1">
          <THEAD>
            <TR>
              <TD width="35%">
                <B>URI</B>
              </TD>
              <TD width="15%">
                <B>Literal</B>
              </TD>
            </TR>
          </THEAD>
          <TBODY>
            <xsl:for-each select="sq:sparql/sq:results/sq:result">
              <TR>
                <TD width="35%">
                  <xsl:value-of select="sq:binding/sq:uri" />
                </TD>
                <TD width="15%">
                  <xsl:value-of select="sq:binding/sq:literal" />
                </TD>
              </TR>
            </xsl:for-each>
          </TBODY>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


标签: html xml xslt