使用XSLT将如下XML转换为下面的HTML显示

gentleboy2009 2013-05-18 07:13:34
使用XSLT将如下XML转换为下面的HTML显示

<Items>
<Item>
<Num>2</Num>
<Price>20</Price>
</Item>
<Item>
<Num>2</Num>
<Price>50</Price>
</Item>
<Item>
<Num>2</Num>
<Price>30</Price>
</Item>
</Items>



<table border="1">
<tr bgcolor="#9acd32">
<th colspan="1" align="left">Total</th>
</tr>
<tr >
<th colspan="1" align="left">40</th>
</tr>
<tr >
<th colspan="1" align="left">100</th>
</tr>
<tr >
<th colspan="1" align="left">60</th>
</tr>
<tr >
<th colspan="1" align="left">200</th>
</tr>
...全文
386 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowhite8888 2013-07-20
  • 打赏
  • 举报
回复
用xslt模板文件
jason_deng 2013-07-01
  • 打赏
  • 举报
回复
price.xml <?xml-stylesheet href="showPrice.xsl" type="text/xsl"?> <Items> <Item> <Num>2</Num> <Price>20</Price> </Item> <Item> <Num>2</Num> <Price>50</Price> </Item> <Item> <Num>2</Num> <Price>30</Price> </Item> <Item> <Num>2</Num> <Price>100</Price> </Item> </Items> showPrice.xsl <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="//Items"> <html> <body> <table border="1"> <tr bgcolor="#9acd32"> <th colspan="1" align="left">Num</th> <th colspan="1" align="left">Price</th> <th colspan="1" align="left">Total</th> </tr> <xsl:for-each select="//Item"> <xsl:variable name='price'> <xsl:value-of select="./Price" /> </xsl:variable> <xsl:variable name="num"> <xsl:value-of select="./Num" /> </xsl:variable> <xsl:variable name="ref2" select="./Price" ></xsl:variable> <tr > <th colspan="1" align="left"><xsl:value-of select="./Num" /></th> <th colspan="1" align="left"><xsl:value-of select="./Price" /></th> <th colspan="1" align="left"> <xsl:value-of select="$price * $num"/></th> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
jason_deng 2013-07-01
  • 打赏
  • 举报
回复
有个问题,不知道为什么<th colspan="1" align="left"> $price * $num </th> 不能转换成乘积的结果,好奇怪。求答案。
jason_deng 2013-07-01
  • 打赏
  • 举报
回复
price.xml <?xml-stylesheet href="showPrice.xsl" type="text/xsl"?> <Items> <Item> <Num>2</Num> <Price>20</Price> </Item> <Item> <Num>2</Num> <Price>50</Price> </Item> <Item> <Num>2</Num> <Price>30</Price> </Item> </Items> showPrice.xsl <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="//Items"> <html> <body> <table border="1"> <tr bgcolor="#9acd32"> <th colspan="1" align="left">Num</th> <th colspan="1" align="left">Price</th> <th colspan="1" align="left">Total</th> </tr> <xsl:for-each select="//Item"> <xsl:variable name='price'> <xsl:value-of select="./Price" /> </xsl:variable> <xsl:variable name="num"> <xsl:value-of select="./Num" /> </xsl:variable> <tr > <th colspan="1" align="left"><xsl:value-of select="./Num" /></th> <th colspan="1" align="left"><xsl:value-of select="./Price" /></th> <!-- below can't transalte the expression ,I don't know why--> <th colspan="1" align="left"> $price * $num </th> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
abc3126759 2013-06-21
  • 打赏
  • 举报
回复
你定义一个xslt模板,里面的数据引用xml里的数据,之后foreach循环一下就好了。

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧