2013-05-27 123 views
0

大家都好,我有一個問題,我是很新的XSLT所以這可能是一個noob問題 這是我的XML結構XSLT頭只顯示一次

<FAQ> 
    <!--About Us--> 
    <Query Section="About Us"> 
     <Question>How do I contact Support?</Question> 
     <Answer> 
      You can connect with us on Twitter or Facebook. 
     </Answer> 
    </Query> 
    <Query Section="About Us"> 
     <Question>Who we are?</Question> 
     <Answer>Will discus it later</Answer> 
    </Query> 
    <Query Section="Another section"> 
     <Question>any question?</Question> 
     <Answer>answer</Answer> 
    </Query> 
</FAQ> 

,這是我的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      <head> 
    </head> 
      <body> 
       <h2>FAQS</h2> 
       <div border="1"> 
        <xsl:for-each select="//*[@Section]"> 
         <xsl:sort select="@Section"/> 
         <ul> 
          <li style="color:blue;cursor:pointer" class="Question"> 
           <xsl:value-of select="Question"/> 
          </li> 
          <li style="display:none" class="answer"> 
           <xsl:value-of select="Answer"/> 
          </li> 
         </ul> 
        </xsl:for-each> 
       </div> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

我收到的每一件事情如預期,但越來越重複我的頭作爲 我得到這樣的事情

  • 關於我們
  • 如何聯繫支持
  • 回答

  • 關於我們

  • 我們是誰?
  • 回答

  • 另一部分

  • 問題
  • 回答

,但我想一些這樣的事

  • 關於我們
  • 如何聯繫支承實t
  • 回答

  • 我們是誰?

  • 回答

  • 另一部分

  • 問題

  • 回答

通知,關於我們的標題來了一次:)希望我已經解釋過的事都好

回答

0

這是ia分組的問題。解決方案是muenchian分組(e.g look to this)。

爲此一鍵添加到您的XSLT:

<xsl:key name="kQuerySection" match="Query" use="@Section"/> 

並有循環。

遍歷組:

<xsl:for-each select="//Query[ 
      generate-id() = generate-id(key('kQuerySection', @Section)[1]) 
       ]"> 

遍歷組成員。

<xsl:for-each select="key('kQuerySection', @Section)"> 

因此試試這個:

<?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" indent="yes"/> 
    <xsl:key name="kQuerySection" match="Query" use="@Section"/> 
    <xsl:template match="/"> 
     <html> 
      <head> 
      </head> 
      <body> 
       <h2>FAQS</h2> 
       <div border="1"> 
        <xsl:for-each select="//Query[ 
            generate-id() = generate-id(key('kQuerySection', @Section)[1]) 
            ]"> 
         <xsl:sort select="@Section"/> 
         <ul> 
          <li> 
           <xsl:value-of select="@Section"/> 
           <xsl:for-each select="key('kQuerySection', @Section)"> 
            <ul> 
             <li style="color:blue;cursor:pointer" class="Question"> 
              <xsl:value-of select="Question"/> 
             </li> 
             <li style="display:none" class="answer"> 
              <xsl:value-of select="Answer"/> 
             </li> 
            </ul> 
           </xsl:for-each> 

          </li> 

         </ul> 

        </xsl:for-each> 
       </div> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝您先生,我有另一個方式做到了這一點,但您的解決方案也正在精做投了你 –