Xslt XSL 1.0的问题<;xsl:for each>;及<;呼叫模板>;
请查看我的代码,并就此向我提供建议: XML文件:Xslt XSL 1.0的问题<;xsl:for each>;及<;呼叫模板>;,xslt,Xslt,请查看我的代码,并就此向我提供建议: XML文件:content.XML: <content> <page id="page-1"> <!-- ... --> <block-center> <block-center-row id="block-center-row-1"> <cd> <title>Empire Burlesque</title> <artis
content.XML
:
<content>
<page id="page-1">
<!-- ... -->
<block-center>
<block-center-row id="block-center-row-1">
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</block-center-row>
<block-center-row id="block-center-row-2">
<block-center-colunm id="block-center-2-1">
<book>
<title>Book Title1</title>
<author>Book Author1</author>
</book>
<book>
<title>Book Title2</title>
<author>Book Author2</author>
</book>
<book>
<title>Book Title3</title>
<author>Book Author3</author>
</book>
</block-center-colunm>
<block-center-colunm id="block-center-2-2">
<seminar>
<author>Seminar author1</author>
<durable>3</durable>
</seminar>
<seminar>
<author>Seminar author2</author>
<durable>1.5</durable>
</seminar>
<seminar>
<author>Seminar author3</author>
<durable>2</durable>
</seminar>
<seminar>
<author>Seminar author4</author>
<durable>3</durable>
</seminar>
</block-center-colunm>
</block-center-row>
</block-center>
</page>
<!-- ... -->
</content>
XSL文件块中心.XSL
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="block-center-row-1">
<div class="block-center-row">
<h2>My CD Collection</h2>
<table border="1" width="100%">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="
content/page[@id=$pageId]/block-center/
block-center-row[@id='block-center-row-1']/cd
">
<tr>
<td>
<xsl:value-of select="title" />
</td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist" />
</td>
</xsl:when>
<xsl:when test="price > 9">
<td bgcolor="#cccccc">
<xsl:value-of select="artist" />
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="artist" />
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</div>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="block-center-1-1.xsl" />
<xsl:template name="block-center">
<!-- if put here. It work properly -->
<div class="block-center">
<xsl:for-each select="content/page[@id=$pageId]/block-center/block-center-row">
<xsl:choose>
<!-- // I does not work here -->
<xsl:when test="@id='block-center-row-1'">
<xsl:call-template name="block-center-row-1" />
</xsl:when>
</xsl:choose>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
如果我调用
内部
循环,我不知道为什么它不工作(不输出数据)。否则没关系。我有几句话要说。首先,在XML中产生格式含义是个坏主意。将元素称为
或
不仅是不必要的冗长,而且当它们的内容不再显示在中间的块中时,还会让你头疼
其次,放开
和
。如果您有过程编程背景,它们可能看起来很方便,但它们是错误的选择。使用
相反,它会生成更干净、更容易理解的代码
现在转到XSL。您的第二个XSL(block center.XSL
)-它做什么?为什么有两个单独的XSL文件?此外,它缺少变量或参数声明。我用过:
<xsl:variable name="pageId" select="'page-1'" />
因此,在for each循环中,您只需执行以下操作:
<xsl:for-each select="cd">
它将开始输出所有
节点。非常感谢您的euthusiasm!
如果我在block-center-1-1.xsl文件中进行更改,它现在可以正常工作
到
或者根据您给我们的信息,这永远不会起作用-根本没有匹配的模板。你需要提供更完整的代码。奇怪。。。右下角写着“6次修订”,但实际上只有3次修订。
content/page[@id=$pageId]/block-center/block-center-row
<xsl:for-each select="cd">