:( Basic question about XML, XSLT(50分)

B

banquan

Unregistered / Unconfirmed
GUEST, unregistred user!
I have a xml list as following (1), how can I trnasfer it to a html as (2)
with XSL? Thanks !!
1. xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
<test>6</test>
<test>7</test>
</root>
2. html file
<html>
</html>
<body>
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
</tr>
</table>
</body>
 
有三种方法可以实现你想要的结果(xmlt->html
1、编写DTD文档类型定义,具体例子可参看有关书籍;
2、用CSS显示
3、用XSl来将xml转换为html具体例子可参看大富翁论坛
 
Come on. Give more details. I want three elements for each line.
That is my problem with XSL.
I can not output '<tr>' or '</tr>' only with XSL.
Any idea?
 
用模板,大概如下:
<xsl:template match="sales">
<xsl:if test="(position() mod 3) = 0">
<TR>
</xsl:if>
<TD><xsl:value-of select="."/></TD>
<xsl:if test="((position() mod 3) = 2) or ((position() mod 3) = last())">
</TR>
</xsl:if>
</xsl:template>
 
<xsl:template match="sales">
应改为
<xsl:template match="test">
 
That is exactly what I did.
The xsl file:
<?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>Attributes</h2>
<table border="1">
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="test">
<xsl:if test="(position() mod 3) = 0">
<TR>
</xsl:if>
<TD><xsl:value-of select="."/></TD>
<xsl:if test="(position() mod 3) = 1 or (position()=last())">
</TR>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The html file
<html>
<body>
<p>Want to see if this works</p>
<script type="text/javascript">// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("test.xml")
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("test.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
 
Sorry, jsxjd, forget to tell you that itdo
es not work because of
<xsl:if test="(position() mod 3) = 0">
<TR>
</xsl:if>
and
<xsl:if test="(position() mod 3) = 1 or (position()=last())">
</TR>
</xsl:if>
<TR> and </TR> are treated as not well-formed tag here. So it is not allowed in XSL.
Any solution for this?
Thanks
 
不能使用WIN98/2K默认的XML Parser,至少应升级到 XML Parser 3.0。
请查看MS的XSLT文档。等会儿我试一下。
 
我已经调试通过,两个文件分别为“AA.xml”和“AA.xsl”。
但我在调试时也发生不正常的情况,这时我重新注册XML3.0,并重启机器。
我这里没有用do
M 和脚本。
注意,标记符不对称时,可用“<xsl:text>”。
另外第一个元素的 position() 值为 1,不是 0 。
祝好运!!!!
文件 AA.xml
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="AA.xsl"?>
<root>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
<test>6</test>
<test>7</test>
</root>
文件 AA.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:eek:utput method='html' />
<xsl:template match="/">
<html><body>
<h2>Attributes</h2>
<table border="1" color="red">
<xsl:apply-templates />
</table>
</body></html>
</xsl:template>
<xsl:template match="test">
<xsl:if test="(position() mod 3) = 1">
<xsl:text disable-output-escaping="yes">
&amp;lt;tr&amp;gt;
</xsl:text>
</xsl:if>
<TD><xsl:value-of select="."/></TD>
<xsl:if test="(position() mod 3) = 0 or (position()=last())">
<xsl:text disable-output-escaping="yes">
&amp;lt;/tr&amp;gt;
</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
以下为XSL的输出,是从浏览器的输出中粘贴过来的:
<html xmlns="http://www.w3.org/TR/REC-html40">
<body>
<h2>Attributes</h2>
<table border="1" color="red">
<tr>
<TD>1</TD>
<TD>2</TD>
<TD>3</TD>
</tr>

<tr>
<TD>4</TD>
<TD>5</TD>
<TD>6</TD>
</tr>

<tr>
<TD>7</TD>
</tr>
</table>
</body>
</html>
 
Hi jsxjd,
It works. Thanks!
The element I missed is <xsl:text>. Thank you for point it out.
 
接受答案了.
 
顶部