2010-02-17 43 views
2

我有以下的問題,我需要的ID是每個promotiom的:如何在XSLT中通過標記名稱檢索兄弟?

這裏是我的XML:

<Promotions> 
     <Promotion> 
     <Category>Arts &amp; Entertainment</Category> 
     <Client>Client 1</Client> 
     <ID>2</ID> 
     <Title>Get your Free 2</Title> 
     </Promotion> 
     <Promotion> 
     <Category>Client 1</Category> 
     <Client>Artsquest</Client> 
     <ID>4</ID> 
     <Title>Get your Free 4</Title> 
     </Promotion> 
     <Promotion> 
     <Category>Client 1</Category> 
     <Client>Artsquest</Client> 
     <ID>5</ID> 
     <Title>Get your Free 5</Title> 
     </Promotion> 
     <Promotion> 
     <Category>Community &amp; Neighborhood</Category> 
     <Client>Client 2</Client> 
     <ID>1</ID> 
     <Title>Get your Free 1</Title> 
     </Promotion> 
     <Promotion> 
     <Category>Education</Category> 
     <Client>Client 3</Client> 
     <ID>3</ID> 
     <Title>Get Your Free 3</Title> 
     </Promotion> 
     <Promotion> 
     <Category>Home &amp; Garden</Category> 
     <Client>Client 4</Client> 
     <ID>6</ID> 
     <Title>Get your Free 6</Title> 
     </Promotion> 
    </Promotions> 

這裏是我的XSLT文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove"> 


     <xsl:output method="html" indent="yes" /> 

     <xsl:key name="categories" match="Category" use="." /> 
     <xsl:key name="client" match="Client" use="." /> 
     <xsl:key name="title" match="Title" use="." /> 

     <xsl:template match="/"> 
     <ul id="red" class="treeview-red"> 
      <xsl:for-each select="/Promotions/Promotion/Category[ 
        generate-id(.) = generate-id(key('categories', .)[1]) 
        ]"> 
      <li> 
     <span> 
      <xsl:value-of select="."/> 
     </span> 

     <ul> 
      <xsl:call-template name="category-client"> 
      <xsl:with-param name="category" select="."/> 
      </xsl:call-template> 
     </ul> 
     </li> 

      </xsl:for-each> 
     </ul> 
     </xsl:template> 

     <xsl:template name="category-client"> 
     <xsl:param name="category" /> 
     <xsl:for-each select="/Promotions/Promotion[Category=$category]/Client[ 
        generate-id(.) = generate-id(key('client', .)[1]) 
        ]"> 
      <li> 
      <span> 
       <xsl:value-of select="."/> 
      </span> 
      <ul> 
       <xsl:call-template name="category-client-title"> 
       <xsl:with-param name="category" select="$category"/> 
       <xsl:with-param name="client" select="."/> 
       </xsl:call-template> 
      </ul> 
      </li> 
     </xsl:for-each> 
     </xsl:template> 

     <xsl:template name="category-client-title"> 
     <xsl:param name="category" /> 
     <xsl:param name="client" /> 
     <xsl:for-each select="/Promotions/Promotion[Category=$category]/Title[ 
        generate-id(.) = generate-id(key('title', .)[1]) 
        ]"> 
      <li> 
     <span> 
      <asp:LinkButton ID ="{/Promotions/Promotion[Category=$category]/ID}" onclick="LinkClicked"> 

     <xsl:value-of select="."/> 
     </asp:LinkButton> 
     </span> 
      </li> 

     </xsl:for-each> 

     </xsl:template> 

    </xsl:stylesheet> 

我的問題是,如果客戶在一個類別中有多個促銷活動輸出ID,我只會得到第一個ID,並且每個都重複一次,那麼改變這種方式的最好方法是什麼,以便該ID與促銷活動所在的行匹配?

這裏是我說的,我根本沒有把握XSL的輸出...

<ul id="red" class="treeview-red" xmlns:asp="remove"> 
    <li><span>Arts &amp; Entertainment</span><ul> 
     <li><span>Client 1</span><ul> 
      <li><span><asp:LinkButton ID="2" onclick="LinkClicked">Get your Free 2</asp:LinkButton></span></li> 
      <li><span><asp:LinkButton ID="2" onclick="LinkClicked">Get your Free 4</asp:LinkButton></span></li> 
      <li><span><asp:LinkButton ID="2" onclick="LinkClicked">Get your Free 5</asp:LinkButton></span></li> 
     </ul> 
     </li> 
    </ul> 
    </li> 
    <li><span>Community &amp; Neighborhood</span><ul> 
     <li><span>Client 2</span><ul> 
      <li><span><asp:LinkButton ID="1" onclick="LinkClicked">Get your Free 1</asp:LinkButton></span></li> 
     </ul> 
     </li> 
    </ul> 
    </li> 
    <li><span>Education</span><ul> 
     <li><span>Client 3</span><ul> 
      <li><span><asp:LinkButton ID="3" onclick="LinkClicked">Get Your Free 3</asp:LinkButton></span></li> 
     </ul> 
     </li> 
    </ul> 
    </li> 
    <li><span>Home &amp; Garden</span><ul> 
     <li><span>Client 4</span><ul> 
      <li><span><asp:LinkButton ID="6" onclick="LinkClicked">Get your Free 6</asp:LinkButton></span></li> 
     </ul> 
     </li> 
    </ul> 
    </li> 
</ul> 

回答

2

在這種情況下正確的XPath是:

<asp:LinkButton ID ="{../ID}" onclick="LinkClicked"> 

既然你是在<xsl:for-each><Title>情況下,你必須去上一級,並從那裏獲取<ID>。你試試

/Promotions/Promotion[Category=$category]/ID 

獲取所有<Promotion>秒的某一類,並從一堆,這是相同的每一次,自然需要第一個ID。

+0

再次感謝湯姆 –

1

只要改變

<asp:LinkButton ID ="{/Promotions/Promotion[Category=$category]/ID}" onclick="LinkClicked"> 

<asp:LinkButton ID ="{../ID}" onclick="LinkClicked"> 
+0

這是我的+1。 – Tomalak

-1

試着這麼做position()=1在您的XSLT中選擇第一個匹配項。