在浏览器中显示xml和xslt [英] display xml and xslt in a browser
问题描述
我有2个文件,file.xml和file.xsl。我正在寻找如何在浏览器中查看结果。从我的研究中,我了解到,只有IE获得了XML解析器,而对于其他浏览器,我需要一个servor脚本。
我也将这些行放在我的xml文件的顶部:
<?xml version =1.0 encoding =UTF-8?>
<?xml-stylesheet type =text / xslhref =Employe.xsl?>
以及我的xsl文件顶部的这些ligne:
<?xml version =1.0encoding =UTF-8?>
< xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
然而,即使我使用IE(11),我也只有文本没有任何风格我已经说了。
我想在表格中显示元素,但没有。
如果有任何我没有找到的错误,我会给你我的表格代码,但通常它是可以的。
$
< xsl:template match =/>
< html>
< body>
< h2>工人清单< / h2>
< table border =1>
< tr bgcolor =#9acd32>
< th rowspan = 3> ID< / th>
< th colspan = 5>基本信息< / th>
< th rowspan = 3>图片< / th>
< th colspan = 2>技能启用< / th>
< / tr>
< tr>
< td rowspan = 2>名称< / td>
< td colspan = 3>地址< / td>
< td rowspan = 2>电话号码< / td>
< td rowspan = 2>技巧1< / td>
< td rowspan = 2>技巧2< / td>
< / tr>
< tr>
< td>号码< / td>
< td>街< / td>
< td>城镇< / td>
< / tr>
< xsl:for-each select =List_Of_Employe / Employe>
< xsl:sort select =Name/>
< tr>
< td>< xsl:value-of select =ID/>< / td>
< td>< xsl:value-of select =Basic_Information / Name/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Number/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Street/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Town/>< / td>
< td>< xsl:value-of select =Basic_Information / Phone_Number/>< td />
< td>< xsl:value-of select =Photo/>< / td>
< td>< xsl:value-of select =Skills_Enable / Skill_1/>< / td>
< td>< xsl:value-of select =Skills_Enable / Skill_2/>< / td>
< / tr>
< / xsl:for-each>
< / table>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>
以下是我的xml代码:
< List_Of_Employe>
< Employe>
< ID> 1235< / ID>
< Basic_Information>
<名称>詹姆斯邦德< / Name>
<地址>
< Number> 05< / Number>
<街>皇后街< / Street>
<城市>伦敦< / Town>
< /地址>
< Phone_Number> 07876543210< /电话号码>
< /基本信息>
<图片> James.png< /照片>
< Skills_Enable>
< Skill_1> XML< / Skill_1>
< Skill_2> C#< / Skill_2>
< / Skills_Enable>
< /雇主>
< Employe>
< ID> 1236< / ID>
< Basic_Information>
<名称>夏洛克福尔摩斯< / Name>
<地址>
< Number> 100< / Number>
<街>王子街< / Street>
<城市>伦敦< / Town>
< /地址>
< Phone_Number> 07765432100< / Phone_Number>
< /基本信息>
<图片> Sherlock.png< /照片>
< Skills_Enable>
< Skill_1> JavaScript< / Skill_1>
< Skill_2> Python< / Skill_2>
< / Skills_Enable>
< /雇主>
< / List_Of_Employe>
希望你能帮助我
Mayeul
您的样式表需要一些改进,但您距离解决方案并不遥远。这不是一个漂亮的解决方案,但它成功地转换了您的输入XML。 (一个更好的解决方案可能会使用多个模板,而不是每个 for 语句的几个模板)。
清除一些东西
您说:
从我的研究中,我了解到只有IE获得了xml解析器,而对于其他浏览器,我需要一个servor脚本
否, 这不是真的。每个主流浏览器都有一个体面的XML解析器。您不需要任何服务器端代码来解析XML。同样,每个主要的浏览器(IE,Chrome,Firefox,Safari)都附带XSLT处理器,不过只支持XSLT 1.0。 b
$ b
甚至当我使用IE浏览器(11)时,我只有文本没有任何风格,我已经把。
输入XML中的文本内容作为转换的结果通常意味着:您的模板都不能匹配输入元素。您的代码没有被应用,并且采取默认操作:调用内置模板,仅输出所有文本节点。
目前还不清楚为什么没有模板应该适用于您的案例,请告诉我下面的样式表是否会改变输出。
我对你的样式表做了以下修改:
< ; td />
应该是< / td>
,因为它结束了 td
元素。
...
)。
一个HTML解析器可以处理很多错误(例如未加引号的属性值),但是XML解析器对此非常严格。
<?xml version =1.0encoding =UTF-8?>
< xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:template match =/>
< html>
< body>
< h2>工人清单< / h2>
< table border =1>
< tr bgcolor =#9acd32>
< th colspan =5>基本信息< / th>
< th colspan =2>技能启用< / th>
< / tr>
< tr>
< td rowspan =2>名称< / td>
< td colspan =3>地址< / td>
< td rowspan =2>电话号码< / td>
< td rowspan =2>技巧1< / td>
< td rowspan =2>技巧2< / td>
< / tr>
< tr>
< td>号码< / td>
< td>街< / td>
< td>城镇< / td>
< / tr>
< xsl:for-each select =List_Of_Employe / Employe>
< xsl:sort select =Name/>
< tr>
< td>< xsl:value-of select =ID/>< / td>
< td>< xsl:value-of select =Basic_Information / Name/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Number/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Street/>< / td>
< td>< xsl:value-of select =Basic_Information / Address / Town/>< / td>
< td>< xsl:value-of select =Basic_Information / Phone_Number/>< / td>
< td>< xsl:value-of select =Photo/>< / td>
< td>< xsl:value-of select =Skills_Enable / Skill_1/>< / td>
< td>< xsl:value-of select =Skills_Enable / Skill_2/>< / td>
< / tr>
< / xsl:for-each>
< / table>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>
另请考虑将以下行添加到样式表中:
< xsl:output method =htmlindent =yes/>
明确表示您希望输出HTML。
XHTML输出
< html>
< body>
< h2>工人清单< / h2>
< table border =1>
< tr bgcolor =#9acd32>
< th colspan =5>基本信息< / th>
< th colspan =2>技能启用< / th>
< / tr>
< tr>
< td rowspan =2>名称< / td>
< td colspan =3>地址< / td>
< td rowspan =2>电话号码< / td>
< td rowspan =2>技巧1< / td>
< td rowspan =2>技巧2< / td>
< / tr>
< tr>
< td>号码< / td>
< td>街< / td>
< td>城镇< / td>
< / tr>
< tr>
< td> 1235< / td>
< td>詹姆斯邦德< / td>
< td> 05< / td>
< td>皇后街< / td>
< td>伦敦< / td>
< td> 07876543210< / td>
< td> James.png< / td>
< td> XML< / td>
< td> C#< / td>
< / tr>
< tr>
< td> 1236< / td>
< td>福尔摩斯< / td>
< td> 100< / td>
< td>王子街< / td>
< td>伦敦< / td>
< td> 07765432100< / td>
< td> Sherlock.png< / td>
< td> JavaScript< / td>
< td> Python< / td>
< / tr>
< / table>
< / body>
< / html>
渲染输出
我没有IE来试用它,但Firefox会将HTML输出呈现为:
最后,让我是短暂的挑剔,它确实应该读员工,而不是雇主。
I have 2 files, file.xml and file.xsl that I have made. I am looking for how to see the result in a browser. From my research I have understood that only IE got an xml parser and that for the others browser I need a servor script. I have also put these lines at the top of my xml file :
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Employe.xsl"?>
and these lignes at the top of my xsl file :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
However, even when i'm using IE (11) I only have the text without any style that I have put. I wanted to display the elemnts in a table but there isn't any.
I am giving you my code for the table if there is any mistakes I haven't found, but normaly it is ok.
Here is the xsl code :
<xsl:template match="/">
<html>
<body>
<h2>List of Workers</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th rowspan=3> ID </th>
<th colspan=5> Basic Information </th>
<th rowspan=3> Picture </th>
<th colspan=2> Skills enable </th>
</tr>
<tr>
<td rowspan=2> Name </td>
<td colspan=3> Address </td>
<td rowspan=2> Phone Number </td>
<td rowspan=2> Skill 1 </td>
<td rowspan=2> Skill 2 </td>
</tr>
<tr>
<td> Number </td>
<td> Street </td>
<td> Town </td>
</tr>
<xsl:for-each select="List_Of_Employe/Employe">
<xsl:sort select="Name"/>
<tr>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="Basic_Information/Name"/></td>
<td><xsl:value-of select="Basic_Information/Address/Number"/></td>
<td><xsl:value-of select="Basic_Information/Address/Street"/></td>
<td><xsl:value-of select="Basic_Information/Address/Town"/></td>
<td><xsl:value-of select="Basic_Information/Phone_Number"/><td/>
<td><xsl:value-of select="Photo"/></td>
<td><xsl:value-of select="Skills_Enable/Skill_1"/></td>
<td><xsl:value-of select="Skills_Enable/Skill_2"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
and here is my xml code :
<List_Of_Employe>
<Employe>
<ID> 1235 </ID>
<Basic_Information>
<Name> James Bond</Name>
<Address>
<Number > 05 </Number>
<Street> Queen's street </Street>
<Town> London </Town>
</Address>
<Phone_Number> 07876543210 </Phone_Number>
</Basic_Information>
<Photo> James.png </Photo>
<Skills_Enable>
<Skill_1> XML </Skill_1>
<Skill_2> C# </Skill_2>
</Skills_Enable>
</Employe>
<Employe>
<ID> 1236 </ID>
<Basic_Information>
<Name> Sherlock Holmes </Name>
<Address>
<Number > 100 </Number>
<Street> Prince's street </Street>
<Town> London </Town>
</Address>
<Phone_Number> 07765432100 </Phone_Number>
</Basic_Information>
<Photo> Sherlock.png </Photo>
<Skills_Enable>
<Skill_1> JavaScript </Skill_1>
<Skill_2> Python </Skill_2>
</Skills_Enable>
</Employe>
</List_Of_Employe>
Hoping you will be able to help me
Mayeul
Your stylesheet needs some improvement, but you're not far off a solution. It's not a beautiful solution, but it successfully transforms your input XML. (A better solution would have used several templates, not a huge one with several for-each
statements).
Clearing up a few things
You say:
From my research I have understood that only IE got an xml parser and that for the others browser I need a servor script
No, that's not true. Every major browser has a decent XML parser. You do not need any server-side code to parse XML. Likewise, every major browser (IE, Chrome, Firefox, Safari) ships with an XSLT processor, though only supporting XSLT 1.0.
However, even when i'm using IE (11) I only have the text without any style that I have put.
Getting nothing but all textual content from the input XML as the result of a transformation usually means: none of your templates could match the input elements. None of your code was applied, and the default action is taken: invoking the built-in templates, that only output all text nodes.
It's unclear to me why no templates should apply in your case, let me know whether the stylesheet I suggest below changes the output.
Stylesheet
I have made the following changes to your stylesheet:
- There was a stray
<td/>
that should be</td>
, because it ends atd
element. - Added quotes to all attribute values. Attribute values absolutely need to be in quotes (
"..."
), since the XSLT code must itself be well-formed XML.
An HTML parser can deal with a lot of errors (e.g. unquoted attribute values), but XML parsers are very strict on this.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>List of Workers</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th rowspan="3"> ID </th>
<th colspan="5"> Basic Information </th>
<th rowspan="3"> Picture </th>
<th colspan="2"> Skills enable </th>
</tr>
<tr>
<td rowspan="2"> Name </td>
<td colspan="3"> Address </td>
<td rowspan="2"> Phone Number </td>
<td rowspan="2"> Skill 1 </td>
<td rowspan="2"> Skill 2 </td>
</tr>
<tr>
<td> Number </td>
<td> Street </td>
<td> Town </td>
</tr>
<xsl:for-each select="List_Of_Employe/Employe">
<xsl:sort select="Name"/>
<tr>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="Basic_Information/Name"/></td>
<td><xsl:value-of select="Basic_Information/Address/Number"/></td>
<td><xsl:value-of select="Basic_Information/Address/Street"/></td>
<td><xsl:value-of select="Basic_Information/Address/Town"/></td>
<td><xsl:value-of select="Basic_Information/Phone_Number"/></td>
<td><xsl:value-of select="Photo"/></td>
<td><xsl:value-of select="Skills_Enable/Skill_1"/></td>
<td><xsl:value-of select="Skills_Enable/Skill_2"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Also consider adding the following line to your stylesheet:
<xsl:output method="html" indent="yes"/>
To make it explicit that you'd like to output HTML.
XHTML Output
<html>
<body>
<h2>List of Workers</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th rowspan="3"> ID </th>
<th colspan="5"> Basic Information </th>
<th rowspan="3"> Picture </th>
<th colspan="2"> Skills enable </th>
</tr>
<tr>
<td rowspan="2"> Name </td>
<td colspan="3"> Address </td>
<td rowspan="2"> Phone Number </td>
<td rowspan="2"> Skill 1 </td>
<td rowspan="2"> Skill 2 </td>
</tr>
<tr>
<td> Number </td>
<td> Street </td>
<td> Town </td>
</tr>
<tr>
<td> 1235 </td>
<td> James Bond</td>
<td> 05 </td>
<td> Queen's street </td>
<td> London </td>
<td> 07876543210 </td>
<td> James.png </td>
<td> XML </td>
<td> C# </td>
</tr>
<tr>
<td> 1236 </td>
<td> Sherlock Holmes </td>
<td> 100 </td>
<td> Prince's street </td>
<td> London </td>
<td> 07765432100 </td>
<td> Sherlock.png </td>
<td> JavaScript </td>
<td> Python </td>
</tr>
</table>
</body>
</html>
Rendered Output
I do not have IE to try it out there, but Firefox renders the HTML output to:
Finally, let me be nit-picking for a brief moment, it really should read "Employee", not "Employe".
这篇关于在浏览器中显示xml和xslt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!