我编写了一个XSL文件,用于以HTML表格格式显示XML文件,但它不起作用。它只显示标题,即uri
和literal
。请仔细阅读我的代码,并用解决方案回复我。
<?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>
我的意见是:
<?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 <b>Institute</b> <b>of</b> Science and <b>Technology</b>, a non profit making trust has been set up to... the technological and professional institution <b>of</b> high standards and to encourage research and... In this era <b>of</b> economic...
</literal>
</binding>
</result>
<result>
<binding name="c1">
<uri>http://dbpedia.org/resource/Haldia_Institute_of_Technology</uri>
</binding>
<binding name="callret-1">
<literal>
<b>Haldia</b> <b>Institute</b> <b>of</b> <b>Technology</b>.
</literal>
</binding>
</result>
</results>
</sparql>
答案 0 :(得分:2)
您需要在样式表中使用XML命名空间来匹配输入文档。
您的输入文档使用默认命名空间
http://www.w3.org/2005/sparql-results#
未明确引用此命名空间(带前缀)的选择器将无效。
您需要像这样添加一个名称空间到样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:spa="http://www.w3.org/2005/sparql-results#">
(请注意,您可以使用您想要的任何前缀。)
然后修改XPath选择器以使用此前缀:
<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元素是小写的。
答案 1 :(得分:0)
Harpo是对的,您只需要在XSLT中包含命名空间并相应地更改XPATH。我已经为你做了这件事:
<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>