XML中的锚标签

时间:2014-05-20 16:46:34

标签: asp.net xml xslt

我有一个XML文件,以2页显示。主页仅显示XML文件的TITLES,秒页面显示所有XML项目,但标题,日期和描述。

当从主页单击标题时,我希望它转到第二页,它直接转到那个呈现为XSLT的ITEM。

我相信使用锚是方法,但我不知道如何使用XML。

XML

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <generator>Easy Feed Editor</generator>
    <title>RSS Feed</title>
    <description>Welcome to the Site</description>
    <link>www.website.com</link>
    <image><url>http://www.website.com/images/logo2.jpg</url><title> RSS Feed</title>           
    <link>www.website.com</link></image>
    <language>en-us</language>
    <pubDate>Tue, 20 May 2014 13:39:17 GMT</pubDate>
    <managingEditor>TCPC</managingEditor>

    <item>
      <title>NEW YEAR'S DAY
      </title>
      <description>
        <![CDATA[<p>
                 We will resume regular business hours on Thursday, January 2, 2014. </p>
        ]]></description>
        <link>http://www.website.com/PT/news.aspx</link>
        <author>PC</author>
        <guid  isPermaLink="false">0d8a3208-0900-45ad-84f4-4934a751aac3</guid>
        <pubDate>Tue, 20 May 2014 13:38:21 GMT</pubDate>
        <enclosure url="http://www.website.com/images/logo2.jpg" type="image/jpeg" length="16645" />
    </item>

    <item>
      <title>SAVE BIG</title>
      <description><![CDATA[<p>
      Pay big description goes here. Sample text. </p>
      ]]></description>
      <link>http://www.website.com/PT/news.aspx</link>
      <author>PC</author>
      <guid  isPermaLink="false">4428636c-218d-46d3-98bd-52e83e27e02f</guid>
      <pubDate>Mon, 16 Dec 2013 13:40:26 GMT</pubDate>
      <enclosure url="http://www.website.com/images/logo2.jpg" type="image/jpeg" length="16645" />
    </item>

    <item>
      <title>NEW OFFICE</title>
      <description><![CDATA[<p>
      Office Description</p>
      ]]></description>
      <link>http://www.website.com/PT/news.aspx</link>
      <author>PC</author>
      <guid  isPermaLink="false">45a6ca63-8bad-4b60-bd0a-69190089b789</guid>
      <pubDate>Fri, 06 Dec 2013 21:34:49 GMT</pubDate>
      <enclosure url="http://www.website.com/images/logo2.jpg" type="image/jpeg" length="16645" />
    </item>

  </channel>
</rss>

XSLT

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>      
      <xsl:for-each select="rss/channel/item">
        <div style="background-color:#a61f2e;color:white;padding:4px; padding-left:1em;">
          <span style="font-weight:bold; font-size: 1.5em;">
            <xsl:value-of select="title"/>
          </span>
        </div>
        <div style="margin-left:20px;margin-bottom:1em; margin-right:1em;">
          <br></br>
          <h5 style="font-style:italic">
            <xsl:value-of select="pubDate"/>
          </h5>
          <br></br>
          <p>
            <span style ="font: normal 14px/1.5 'Segoe UI'">
              <xsl:value-of select="description"/>
            </span>
          </p>
        </div>
      </xsl:for-each>     
    </html>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

锚点和超链接将成为目标HTML的一部分,您不会将其真正编码到XML中。 XML只是作为XSLT生成它们的源。

要实现您的目标,您必须对项目进行两次迭代:一次生成带有超链接的第一页,另一次生成带有命名锚的第二页。您可以使用generate-id()函数为锚点生成唯一ID。以下XSLT试图模仿这个。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="/">
    <html>      
      <H1>page1</H1>
      <UL>
        <xsl:for-each select="rss/channel/item">

          <!-- generate a hyperlink to a named anchor on the same page -->
          <LI><A HREF="#{generate-id()}"><xsl:value-of select="title"/></A></LI>
        </xsl:for-each>
      </UL>

      <H1>page2</H1>
        <xsl:for-each select="rss/channel/item">

          <!-- {{{{{ generate a named anchor using the same id -->
          <A NAME="{generate-id()}">
            <div style="background-color:#a61f2e;color:white;padding:4px; padding-left:1em;">
              <span style="font-weight:bold; font-size: 1.5em;">
                <xsl:value-of select="title"/>
              </span>
            </div>
          </A>
          <!-- }}}}} -->

          <div style="margin-left:20px;margin-bottom:1em; margin-right:1em;">
            <br></br>
            <h5 style="font-style:italic">
              <xsl:value-of select="pubDate"/>
            </h5>
            <br></br>
            <p>
              <span style ="font: normal 14px/1.5 'Segoe UI'">
                <xsl:value-of select="description"/>
              </span>
            </p>
          </div>
        </xsl:for-each>     
    </html>
  </xsl:template>
</xsl:stylesheet>