如何使用 xslt 将 XML(从数据集生成)转换为 HTML
尝试从xml中获取html 我已经从数据集生成了以下 xml,这意味着我无法更改 XML 的结构。我认为我错过了循环 A 和 B 的第二个循环。
<Myds>
<A>
<col1>Row1</col1>
<col2>1</col2>
<col3>2</col3>
</A>
<A>
<col1>Row2</col1>
<col2>4</col2>
<col3>3</col3>
</A>
<B>
<col1>Row1</col1>
<col2>1</col2>
<col3>2</col3>
</B>
<B>
<col1>Row2</col1>
<col2>4</col2>
<col3>3</col3>
</B>
</Myds>
这是 XSL
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<xsl:for-each select ='./Myds'>
<table>
<xsl:for-each select ='A'>
<tr>
<td><xsl:value-of select='col1'/></td>
<td><xsl:value-of select='col2'/></td>
<td><xsl:value-of select='col3'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我得到了这个输出
<?xml version="1.0" encoding="utf-8"?>
<table>
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
但是我期待这个
<table>//(For Table A)
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
<table>//(For Table B)
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
我想我需要这样的东西但不知道如何......
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<xsl:for-each select ='./Myds'>
<table>
<xsl:for-each select ='UNIQUE SECOND NODE'>
<tr>
<td><xsl:value-of select='col1'/></td>
<td><xsl:value-of select='col2'/></td>
<td><xsl:value-of select='col3'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Trying to get the html from the xml
I have generated following xml from a datset which means that I cannot change the structure of XML.I think I am missing on a second loop that loop through the A and B .
<Myds>
<A>
<col1>Row1</col1>
<col2>1</col2>
<col3>2</col3>
</A>
<A>
<col1>Row2</col1>
<col2>4</col2>
<col3>3</col3>
</A>
<B>
<col1>Row1</col1>
<col2>1</col2>
<col3>2</col3>
</B>
<B>
<col1>Row2</col1>
<col2>4</col2>
<col3>3</col3>
</B>
</Myds>
This is the XSL
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<xsl:for-each select ='./Myds'>
<table>
<xsl:for-each select ='A'>
<tr>
<td><xsl:value-of select='col1'/></td>
<td><xsl:value-of select='col2'/></td>
<td><xsl:value-of select='col3'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have got this output
<?xml version="1.0" encoding="utf-8"?>
<table>
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
But I was expecting this
<table>//(For Table A)
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
<table>//(For Table B)
<tr><td>Row1</td><td>1</td><td>2</td></tr>
<tr><td>Row2</td><td>4</td><td>3</td></tr>
</table>
I think I need something like this but don't exactly how...
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<xsl:for-each select ='./Myds'>
<table>
<xsl:for-each select ='UNIQUE SECOND NODE'>
<tr>
<td><xsl:value-of select='col1'/></td>
<td><xsl:value-of select='col2'/></td>
<td><xsl:value-of select='col3'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xslt 区分大小写;对于您需要的每一行
,您需要使用 Muenchian 分组来为每个类似命名的元素获取一个
:
xslt is case sensitive; for each row you'll need
and you'll need to use Muenchian grouping to get one
<table>
per like-named elements: