<div>
不能是<xml:choose>
的孩子,但我只想重复<li>
次 - 换句话说,不是他们的父<div>
或{{1} }}第
下面你会找到我的样本输入,XSLT和样本所需的输出。
<ul>
<?xml version="1.0" encoding="utf-8" ?>
<Collection>
<Content>
<Html>
<root>
<news>
<title>Item 1</title>
<publication-date>2010-04-16</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 2</title>
<publication-date>2010-04-19</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
</Collection>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="/Collection/Content" xml:space="preserve">
<div id="latest-news" class="module clear">
<h2>Latest News</h2>
<xsl:choose>
<xsl:when test="position() = 1">
<div id="featured-story">
<h3>
<a href="#tk">
<xsl:copy-of select="Html/root/news/title/node()" />
</a>
</h3>
<p class="publish-date">
<xsl:copy-of select="Html/root/news/publication-date/node()" />
</p>
<xsl:copy-of select="Html/root/news/article-content/node()" />
<p class="more">
<a href="#tk">Read more</a>
</p>
</div>
</xsl:when>
<xsl:when test="position() > 1 and position() < 6">
<div id="summaries" class="column-1">
<ul>
<li>
<h3>
<a href="#tk">
<xsl:copy-of select="Html/root/news/title/node()" />
</a>
</h3>
<p class="publish-date">
<xsl:copy-of select="Html/root/news/publication-date/node()" />
</p>
<xsl:copy-of select="Html/root/news/article-content/node()" />
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
</ul>
</div>
</xsl:when>
<xsl:otherwise>
<div id="links" class="column-2">
<ul>
<li>
<p class="publish-date">
<xsl:copy-of select="Html/root/news/publication-date/node()" />
</p>
<h3>
<a href="#tk">
<xsl:copy-of select="Html/root/news/title/node()" />
</a>
</h3>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
</ul>
</div>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
你的结构错了。这是一个语法上有效(但未经测试)的重构。我没有测试,因为你没有发布一些输入XML,但我认为这就是你想要的。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="/Collection" xml:space="preserve">
<div id="latest-news" class="module clear">
<h2>Latest News</h2>
<div id="featured-story">
<h3>
<a href="#tk">
<xsl:copy-of select="Content[1]/Html/root/news/title/node()"/>
</a>
</h3>
<p class="publish-date">
<xsl:copy-of select="Content[1]/Html/root/news/publication-date/node()"/>
</p>
<xsl:copy-of select="Content[1]/Html/root/news/article-content/node()"/>
<p class="more">
<a href="#tk">Read more</a>
</p>
</div>
<div id="summaries" class="column-1">
<ul>
<xsl:apply-templates select="Content[position() > 1 and position() < 6]"/>
</ul>
</div>
<div id="links" class="column-2">
<ul>
<xsl:apply-templates select="Content[position() > 5]"/>
</ul>
</div>
</div>
</xsl:template>
<xsl:template match="Content" xml:space="preserve">
<li>
<h3>
<a href="#tk">
<xsl:copy-of select="Html/root/news/title/node()"/>
</a>
</h3>
<p class="publish-date">
<xsl:copy-of select="Html/root/news/publication-date/node()"/>
</p>
<xsl:copy-of select="Html/root/news/article-content/node()"/>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="Collection">
<div id="latest-news" class="module clear">
<h2>Latest News</h2>
<div id="featured-story">
<xsl:apply-templates select="Content[1]"/>
</div>
<div id="summaries" class="column-1">
<ul>
<xsl:apply-templates select="Content[position() > 1 and 6 > position()]"/>
</ul>
</div>
<div id="links" class="column-2">
<ul>
<xsl:apply-templates select="Content[position() > 5]"/>
</ul>
</div>
</div>
</xsl:template>
<xsl:template match="Content[position() != 1]">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template match="news">
<xsl:apply-templates/>
<p class="more">
<a href="#tk">Read more</a>
</p>
</xsl:template>
<xsl:template match="Content[position() > 5]/*/*/news">
<xsl:apply-templates select="title|publication-date" />
</xsl:template>
<xsl:template match="title">
<h3>
<a href="#tk">
<xsl:value-of select="." />
</a>
</h3>
</xsl:template>
<xsl:template match="publication-date">
<p class="publish-date">
<xsl:value-of select="." />
</p>
</xsl:template>
<xsl:template match="article-content">
<p>
<xsl:value-of select="substring(.,1,15)" />
</p>
</xsl:template>
<xsl:template match="Content[1]/*/*/*/article-content">
<xsl:copy-of select="node()" />
</xsl:template>
</xsl:stylesheet>
输入:
<Collection>
<Content>
<Html>
<root>
<news>
<title>Item 1</title>
<publication-date>2010-04-13</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 2</title>
<publication-date>2010-04-14</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 3</title>
<publication-date>2010-04-15</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 4</title>
<publication-date>2010-04-16</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 5</title>
<publication-date>2010-04-17</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 6</title>
<publication-date>2010-04-18</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
<Content>
<Html>
<root>
<news>
<title>Item 7</title>
<publication-date>2010-04-19</publication-date>
<article-content>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
</article-content>
</news>
</root>
</Html>
</Content>
</Collection>
结果:
<div id="latest-news" class="module clear">
<h2>Latest News</h2>
<div id="featured-story">
<h3>
<a href="#tk">Item 1</a>
</h3>
<p class="publish-date">2010-04-13</p>
<p>Aliquam mollis porttitor auctor. Aenean laoreet justo sed ipsum lobortis eleifend. In ac mollis neque. Donec rutrum turpis vel quam mattis eu vestibulum arcu tincidunt.</p>
<p class="more">
<a href="#tk">Read more</a>
</p>
</div>
<div id="summaries" class="column-1">
<ul>
<li>
<h3>
<a href="#tk">Item 2</a>
</h3>
<p class="publish-date">2010-04-14</p>
<p>Aliquam mollis </p>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
<li>
<h3>
<a href="#tk">Item 3</a>
</h3>
<p class="publish-date">2010-04-15</p>
<p>Aliquam mollis </p>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
<li>
<h3>
<a href="#tk">Item 4</a>
</h3>
<p class="publish-date">2010-04-16</p>
<p>Aliquam mollis </p>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
<li>
<h3>
<a href="#tk">Item 5</a>
</h3>
<p class="publish-date">2010-04-17</p>
<p>Aliquam mollis </p>
<p class="more">
<a href="#tk">Read more</a>
</p>
</li>
</ul>
</div>
<div id="links" class="column-2">
<ul>
<li>
<h3>
<a href="#tk">Item 6</a>
</h3>
<p class="publish-date">2010-04-18</p>
</li>
<li>
<h3>
<a href="#tk">Item 7</a>
</h3>
<p class="publish-date">2010-04-19</p>
</li>
</ul>
</div>
</div>
注意:这样您就可以获得所需内容。在您的模板中“模式匹配”您的输入,您可以更好地表达您想要的输出并增加可重用性和维护的可能性。
修改:稍加改动以强调“模式匹配”。