如何在XSLT中按标记名称检索兄弟?

时间:2010-02-17 14:31:49

标签: xslt

我遇到以下问题,我需要每个发起人的ID:

这是我的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>

3 个答案:

答案 0 :(得分:2)

在这种情况下正确的XPath是:

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

由于您位于<Title>内的<xsl:for-each>上下文中,因此您必须上升一级并从那里获取<ID>。你的尝试

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

获取某个类别的所有 <Promotion>并从该群中获取第一个ID,这自然是相同的。

答案 1 :(得分:1)

只需更改

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

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

答案 2 :(得分:-1)

在XSLT中尝试使用类似position()=1的内容来选择第一个匹配项。