2010-02-17 42 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中选择第一个匹配项。