0

これが私のフィードです

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="feedstylev5.xsl"?>
<feed >

   <Layout>
        <breakingnews>
               <story id="112345" rank="1">Story Title 1</story>
                <story id="122345" rank="2">Story Title 1</story>
                <story id="212345" rank="3">Story Title 2</story>

        </breakingnews>

        <topnews>
                <story id="012345" rank="1">Story Title 1</story>
                <story id="117345" rank="2">Story Title 1</story>
                <story id="612345" rank="3">Story Title 1</story>
                <story id="712345" rank="4">Story Title 1</story>

        </topnews>

        <news>
                <story id="012345" rank="1">Story Title 1</story>
                <story id="117345" rank="2">Story Title 1</story>
                <story id="612345" rank="3">Story Title 1</story>
                <story id="312145" rank="4">Story Title 1</story>
                <story id="412045" rank="5">Story Title 1</story>
        </news>

        <sports>
                <story id="712345" rank="1">Story Title 1</story>
                <story id="912345" rank="2">Story Title 1</story>
                <story id="812345" rank="3">Story Title 1</story>
                <story id="102345" rank="4">Story Title 1</story>
                <story id="212245" rank="5">Story Title 1</story>
        </sports>



   </Layout>
</feed>

私がやろうとしているのは、ストーリーをループし、ニュース速報、スポーツタグを以下のようにハードコーディングするのではなく、タグ自体を表示したい場所です。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom">

<xsl:template match="/feed">
  <html>
  <body>


   <h2 style="font-style:italic; font-weight:bold;">Breaking News</h2>

    <xsl:for-each select="Layout/breakingnews/story">

      <div style="float:left; margin-left:25px; margin-right:5px;"><xsl:value-of select="@rank"/></div>
      <div style="float:left;"> <xsl:value-of select="."/></div>
      <div style="float:left;">(<xsl:value-of select="@id"/>)</div>
      <div style="clear:both;"></div>    
    </xsl:for-each>




  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

値が変化し、ループさせたいので、ニュース速報を変数に置き換えたいと思います。

4

2 に答える 2

2

私の推奨事項は、次のようなことを行うことです - for-each ロジックをテンプレートに移動します:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom">

    <xsl:template match="/feed">
        <html>
            <body>
                <h2 style="font-style:italic; font-weight:bold;">Breaking News</h2>
                <xsl:apply-templates select="Layout/breakingnews/story"/>

                <h2 style="font-style:italic; font-weight:bold;">Top News</h2>
                <xsl:apply-templates select="Layout/topnews/story"/>

            </body>
        </html>
    </xsl:template>

    <xsl:template match="story">
        <div style="float:left; margin-left:25px; margin-right:5px;">
            <xsl:value-of select="@rank"/>
        </div>
        <div style="float:left;">
            <xsl:value-of select="."/>
        </div>
        <div style="float:left;">
            (<xsl:value-of select="@id"/>)
        </div>
        <div style="clear:both;"></div>                
    </xsl:template>

</xsl:stylesheet>
于 2013-01-07T17:25:13.723 に答える
0

XML 構造を変更することは可能ですか? その場合、XML を次のように変更することをお勧めします。

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="feedstylev5.xsl"?>
<feed >

   <Layout>
        <section code="breakingnews" title="Breaking News">
               <story id="112345" rank="1">Story Title 1</story>
                <story id="122345" rank="2">Story Title 1</story>
                <story id="212345" rank="3">Story Title 2</story>

        </section>

        <section code="topnews" title="Top News">
                <story id="012345" rank="1">Story Title 1</story>
                <story id="117345" rank="2">Story Title 1</story>
                <story id="612345" rank="3">Story Title 1</story>
                <story id="712345" rank="4">Story Title 1</story>

        </section>

        ...  

   </Layout>
</feed>

次に、XSLT を次のようにします。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom">
  <xsl:template match="/feed">
    <html>
      <body>

        <xsl:apply-templates select="Layout/section" />

      </body>
    </html>
  </xsl:template>

  <xsl:template match="section">
    <h2 style="font-style:italic; font-weight:bold;">
      <xsl:value-of select="@title" />
    </h2>
    <xsl:apply-templates select="story" />
  </xsl:template>

  <xsl:template match="story">
    <div style="float:left; margin-left:25px; margin-right:5px;">
      <xsl:value-of select="@rank"/>
    </div>
    <div style="float:left;">
      <xsl:value-of select="."/>
    </div>
    <div style="float:left;">
      (<xsl:value-of select="@id"/>)
    </div>
    <div style="clear:both;"></div>
  </xsl:template>
</xsl:stylesheet>

code= 属性は上記では実際には使用されていませんが、適切な手段として使用することをお勧めします。

入力 XML 構造を変更できない場合、次の最善の策は、この例の最初の 2 つのテンプレートを次のように変更することです。

  <xsl:template match="/feed">
    <html>
      <body>

         <xsl:call-template name="Section">
           <xsl:with-param name="section" select="Layout/breakingnews" />
           <xsl:with-param name="title" select="'Breaking News'" />
         </xsl:call-template>
         <xsl:call-template name="Section">
           <xsl:with-param name="section" select="Layout/topnews" />
           <xsl:with-param name="title" select="'Top News'" />
         </xsl:call-template>
         <xsl:call-template name="Section">
           <xsl:with-param name="section" select="Layout/news" />
           <xsl:with-param name="title" select="'News'" />
         </xsl:call-template>
         <xsl:call-template name="Section">
           <xsl:with-param name="section" select="Layout/sports" />
           <xsl:with-param name="title" select="'Sports'" />
         </xsl:call-template>

      </body>
    </html>
  </xsl:template>

  <xsl:template name="Section">
    <xsl:param name="section" />
    <xsl:param name="title" />
    <h2 style="font-style:italic; font-weight:bold;">
      <xsl:value-of select="$title" />
    </h2>
    <xsl:apply-templates select="story" />
  </xsl:template>

これにより、特定の時点でフィードにないセクションは省略されますが、XSL でタイトルをハードコーディングする必要があり、その順序は固定されます。

于 2013-01-08T04:08:40.087 に答える