tags:

views:

56

answers:

2
A: 

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"&gt;
  <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() &gt; 1 and position() &lt; 6]"/>
          </ul>
        </div>

        <div id="links" class="column-2">
          <ul>
            <xsl:apply-templates select="Content[position() &gt; 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>
Jim Garrison
Jim, thanks! When you wrote "wrong", did you mean inefficient? Or something else entirely? This is my first attempt at XSLT, so I'm doing my best to learn best practices now. And, as for your solution, it gets me very close. I intended for `#links` to not include `article-content`, but this is but a minor quibble. Thanks again!
jareha
Your structure could never be used to accommodate your requirement that only the `<li>` tags repeat. Analyze the difference between yours and mine and you'll see what was needed. If you want to specialize the `Content` template, read up on the `mode=` parameter (on both `template` and `apply-templates) to set up two variants that output different HTML.
Jim Garrison
Also BTW, if the answer works for you it's customary to "accept" it by clicking the check mark at the left of the answer.
Jim Garrison
Accepted. I'll look into the `mode` parameter now. Thanks!
jareha
And, following up, `mode` worked perfectly. Thanks for pointing me in the right direction there.
jareha
You're welcome, glad I could help.
Jim Garrison
A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <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".

Alejandro
This is great. In addition to the reusablility you've factored in, I also see a couple new (to me) features I can start utilizing, such as: `/*`, for wildcards; and `substring(.,1,15)`, for string selection. Thanks!
jareha