我正在尝试将xml文件转换为xslt
我的xml文件是这样的,
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="testxsl.xsl"?>
<doc>
<test>
<field name="id">c8b102</field>
<field name="created_at">2013-11-14T00:00:00Z</field>
<field name="userid">userfirst</field>
<field name="screenname">userfirst</field>
<field name="status">open</field>
<field name="contents">testing content</field>
<field name="sentiment">positive</field>
<field name="location">placename</field>
<field name="source">local</field>
<field name="url">test</field>
<field name="keyword">test</field>
</test>
<test>
<field name="id">c8b102</field>
<field name="created_at">2013-11-14T00:00:00Z</field>
<field name="userid">usersec</field>
<field name="screenname">usersec</field>
<field name="status">close</field>
<field name="contents">test sec</field>
<field name="sentiment">positive</field>
<field name="location">place</field>
<field name="source">local</field>
<field name="url">test</field>
<field name="keyword">test</field>
</test>
</doc>
我已经为转换编写了一个xsl代码(testxsl.xsl)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Complaints</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Created Date</th>
<th>UserID</th>
<th>Screenname</th>
<th>Status</th>
<th>Contents</th>
<th>Source</th>
<th>URL</th>
</tr>
<xsl:for-each select="doc/test">
<tr>
<td>
<xsl:value-of select="date" />
</td>
<td>
<xsl:value-of select="str[@name = 'userid']" />
</td>
<td>
<xsl:value-of select="str[@name='screenname']" />
</td>
<td>
<xsl:value-of select="str[@name='status']" />
</td>
<td>
<xsl:value-of select="str[@name='contents']" />
</td>
<td>
<xsl:value-of select="str[@name='source']" />
</td>
<td>
<xsl:value-of select="str[@name='url']" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
但我只在带边框的输出中获得标题部分,如下所示,
Created Date UserID Screenname Status Contents Source URL
我不知道是什么问题,我的xsl文件似乎是正确的
答案 0 :(得分:2)
输入XML中没有名为date
的元素。同样,test
中的元素不会被称为str
,而是field
。
您的XSLT代码当然必须与输入XML中的元素相对应。
使用以下样式表:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Complaints</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Created Date</th>
<th>UserID</th>
<th>Screenname</th>
<th>Status</th>
<th>Contents</th>
<th>Source</th>
<th>URL</th>
</tr>
<xsl:for-each select="doc/test">
<tr>
<td>
<xsl:value-of select="field[@name='created_at']" />
</td>
<td>
<xsl:value-of select="field[@name = 'userid']" />
</td>
<td>
<xsl:value-of select="field[@name='screenname']" />
</td>
<td>
<xsl:value-of select="field[@name='status']" />
</td>
<td>
<xsl:value-of select="field[@name='contents']" />
</td>
<td>
<xsl:value-of select="field[@name='source']" />
</td>
<td>
<xsl:value-of select="field[@name='url']" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<html>
<body>
<h2>Complaints</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Created Date</th>
<th>UserID</th>
<th>Screenname</th>
<th>Status</th>
<th>Contents</th>
<th>Source</th>
<th>URL</th>
</tr>
<tr>
<td>2013-11-14T00:00:00Z</td>
<td>userfirst</td>
<td>userfirst</td>
<td>open</td>
<td>testing content</td>
<td>local</td>
<td>test</td>
</tr>
<!--...-->
</table>
</body>
</html>
呈现HTML