使用XSLT将XML数据转换为HTML表格 [英] Turn XML data to HTML table with XSLT
问题描述
我需要能够将一个平坦的xml数据集转换为html表格,并且我无法找到符合我需要的语法示例。我想使用一个样式表,可以将类似的外观数据集转换为具有可变列的html表格。
这是我的XML文件的一部分:
<?xml version =1.0encoding =utf -8\" >?; <?xml-stylesheet type =text / xslhref =XSLT_StyleSheet.xsl?> <服务>
< Service WsdlUrl =http://venus.eas.asu.edu/WSRepository/Services/BasicThreeSvc/Service.svc>
<名称>服务名称< /名称>
< Provider>< / Provider>
< Category> CatName< / Category>
<操作>
<操作>
<名称> HelloWorld< /名称>
< MsgIn> elloWorldInputMessage< / MsgIn>
< MsgOut> HelloWorldOutputMessage< / MsgOut>
< / Operaion>
<操作>
<名称> OP2name< /名称>
< MsgIn> InputMessage< / MsgIn>
< MsgOut> OutputMessage< / MsgOut>
< / Operaion>
<操作>
<名称> Op3Name< /名称>
< MsgIn> InputMessage< / MsgIn>
< MsgOut> OutputMessage< / MsgOut>
< / Operaion>
< /运营>
HTML表必须看起来像:
<?xml version =1.0encoding =utf-8?> ;
< xsl:output omit-xml-declaration =yesindent =yes/>
< xsl:template match =node()| @ */>
< xsl:template match =/ Services>
< html>
< head>
< title> XSLT示例< / title>
< / head>
< body>
< xsl:apply-templates />
< / body>
< / html>
< / xsl:template>
< xsl:template match =服务>
< xsl:apply-templates />
< / xsl:template>
< xsl:template match =操作>
< table>
< thead>
< tr>
< td>名称< / td>
< td>说明< / td>
< td>类型< / td>
< / tr>
< / thead>
< tbody>
< xsl:apply-templates />
< / tbody>
< / table>
< / xsl:template>
< xsl:template match =操作> <! - [原文] - >
< xsl:variable name =serviceselect =ancestor :: Service/>
< tr>
< td>< xsl:value-of select =$ service / Name/>< / td>
< td>< xsl:value-of select =Name/>< / td>
< td>< xsl:value-of select =$ service / Category/>< / td>
< / tr>
< / xsl:template>
< / xsl:transform>
您的(已更正)文件(缺少结束标记)的输出:
< html>
< head>
< title> XSLT示例< / title>
< / head>
< body>
< table>
< thead>
< tr>
< td>名称< / td>
< td>说明< / td>
< td>类型< / td>
< / tr>
< / thead>
< tbody>
< tr>
< td> ServiceName< / td>
< td> HelloWorld< / td>
< td> CatName< / td>
< / tr>
< tr>
< td> ServiceName< / td>
< td> OP2name< / td>
< td> CatName< / td>
< / tr>
< tr>
< td> ServiceName< / td>
< td> Op3Name< / td>
< td> CatName< / td>
< / tr>
< / tbody>
< / table>
< / body>
< / html>
I need to be able to turn a flat xml data sets into html tables, and I'm having trouble finding syntax examples that will fit my need. I would like to use one stylesheet that can convert similar looking data sets into html tables with variable columns. this is a part of my XML File :
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="XSLT_StyleSheet.xsl"?> <Services>
<Service WsdlUrl="http://venus.eas.asu.edu/WSRepository/Services/BasicThreeSvc/Service.svc">
<Name>ServiceName</Name>
<Provider></Provider>
<Category>CatName</Category>
<Operations>
<Operaion>
<Name>HelloWorld</Name>
<MsgIn>elloWorldInputMessage</MsgIn>
<MsgOut>HelloWorldOutputMessage</MsgOut>
</Operaion>
<Operaion>
<Name>OP2name</Name>
<MsgIn>InputMessage</MsgIn>
<MsgOut>OutputMessage</MsgOut>
</Operaion>
<Operaion>
<Name>Op3Name</Name>
<MsgIn>InputMessage</MsgIn>
<MsgOut>OutputMessage</MsgOut>
</Operaion>
</Operations>
this how the HTML table must look Like:
If you did not find examples of transforming XML to HTML with XSLT, then you didn't look very hard. That's one of its primary motivations. Anyway, this should get you started:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*"/>
<xsl:template match="/Services">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Service">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Operations">
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</xsl:template>
<xsl:template match="Operaion"> <!-- [sic] -->
<xsl:variable name="service" select="ancestor::Service"/>
<tr>
<td><xsl:value-of select="$service/Name"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="$service/Category"/></td>
</tr>
</xsl:template>
</xsl:transform>
Output on your (corrected) document (it was missing end tags):
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<tr>
<td>ServiceName</td>
<td>HelloWorld</td>
<td>CatName</td>
</tr>
<tr>
<td>ServiceName</td>
<td>OP2name</td>
<td>CatName</td>
</tr>
<tr>
<td>ServiceName</td>
<td>Op3Name</td>
<td>CatName</td>
</tr>
</tbody>
</table>
</body>
</html>
这篇关于使用XSLT将XML数据转换为HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!