A <div>
cannot be a child of <xml:choose>
, but I only want <li>
s to repeat — in other words, not their parent <div>
s or <ul>
s.
Below you'll find my sample input, XSLT, and sample desired output.
Input
<?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>
XSLT
<?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>
Output
<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">July 7, 2010</p>
<p>[Article content, in form of an extended summary]</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">July 7, 2010</p>
<p>[Article content, in short summary format]</p>
</li>
<li>
<h3><a href="#tk">[Item 3 and so on]</a></h3>
<p class="publish-date">July 7, 2010</p>
<p>[Article content, in short summary format]</p>
</li>
</ul>
</div>
<div id="links" class="column-2">
<ul>
<li>
<h3><a href="#tk">[Item 6]</a></h3>
<p class="publish-date">July 7, 2010</p>
</li>
<li>
<h3><a href="#tk">[Item 7 and so on]</a></h3>
<p class="publish-date">July 7, 2010</p>
</li>
</ul>
</div>
</div>
Your structure is wrong. Here's a syntactically valid (but untested) refactoring. I didn't test since you didn't post some input XML, but I think this is what you want.
<?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>
This stylesheet:
<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>
Input:
<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>
Result:
<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>
Note: This way you get exactly what you want. "Pattern matching" your input in your templates you can express better your desired output and add possibility of reusability and maintenance.
Edit: Minor change in order to emphasize "pattern matching".
精彩评论