使用XSL转换将XML转换为HTML [英] Using XSL transformation for XML to HTML conversion

查看:77
本文介绍了使用XSL转换将XML转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个XSL文件来显示HTML表格格式的XML文件,但它不起作用。它只显示标题,即 uri literal

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>


推荐答案

您需要在样式表中使用XML Namespaces您的输入文档。

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.)

然后修改XPath选择器以使用此前缀:

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>

另外,请使用缩进并注意HTML元素是小写。

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

这篇关于使用XSL转换将XML转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆