使用XSL转换将XML转换为HTML [英] Using XSL transformation for XML to HTML conversion
问题描述
我已经编写了一个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 <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>
推荐答案
您需要在样式表中使用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屋!