2011-05-24 36 views
0

我有下面的XML:子串並在XSLT分組問題2.0

<InterSection criteria="Microsoft-ASP">value</Intersection> 
<InterSection criteria="Microsoft-MVC">value</Intersection> 
<InterSection criteria="HP-MVC">value</Intersection> 

我需要在我的HTML頁面的頂部創建TOC,所以我需要按標準(前第一部分「 - 」) 。

我需要一個像

  1. 不同的值微軟
  2. HP

我的XSLT有下面的代碼

<a> 
    <xsl:attribute name="href"> 
     <xsl:text>#trigger</xsl:text> 
     <xsl:value-of select="position()"/> 
    </xsl:attribute> 
       <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')"> 
         <xsl:value-of select="substring-before(@Criteria, ' - ')"/> 
         <xsl:text> 
         </xsl:text> 
        </xsl:for-each-group>   

    </a> 

但不知何故,我得到

  1. 微軟
  2. 微軟
  3. HP

,而不是 1.微軟 2. HP

我有以下寫在我的XSLT

<xsl:template match="InterSection" mode="TOC"> 

<li> 
    <a> 
    <xsl:attribute name="href"> 
     <xsl:text>#trigger</xsl:text> 
     <xsl:value-of select="position()"/> 
    </xsl:attribute> 

    <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')"> 
     <xsl:value-of select="current-grouping-key()"/> 
     <xsl:for-each select="current-group()"/> 
    </xsl:for-each-group> 
    </a> 
</li> 

+0

你的工作是一團糟:案件不保留(例如, 'InterSection'用'Intersection'關閉),你在模板中使用'Criteria',而在輸入時你有'criteria',你用子空間(' - ')來取。連字符(' - ')之間沒有空格。 – 2011-05-24 20:16:07

+0

我終於嘗試總結了所有我將解決的問題,以使其工作。 – 2011-05-25 05:13:52

回答

1

我終於要總結我會解決你的工作,以便在問題,使其工作(刪除以前編輯):

  • 您輸入XML元素不正確的情況下
  • 在你改變你參考屬性criteria使用了錯誤的情況下
  • substring表達式包含圍繞第二個參數太多空間-
  • 你應該使用current-grouping-key()獲得電流匹配組的值
  • 模板必須的InterSectionRoot)母公司匹配,然後你必須反覆InterSection像你這樣有xsl:for-each
+0

我做到了,但它並沒有給我獨特的價值 – jvm 2011-05-24 20:15:02

+0

很難理解你做錯了什麼,從你提供的片段開始。請確保按照我上面評論中的建議清理您的代碼,並檢查我的編輯以查看分組是如何工作的。希望能幫助到你。 – 2011-05-24 20:25:26

+0

我修改了原始問題的代碼示例,仍然得到相同的結果 – jvm 2011-05-24 20:33:16

1
  <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')"> 

這是沒有意義的事情。 select屬性中的表達式選擇單個項目。當有多個項目選擇時,分組具有意義,我們希望使用特定標準對它們進行分組。

當單個項目「分組」時,「結果」始終是同一項目 - 這正是您所得到的。

解決方案:選擇正確的一組項目進行分組。或者,使用這種替代的解決方案:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vallKeys" select= 
    "distinct-values 
     (/*/*/@criteria 
      /substring-before(.,'-') 
    ) 
    "/> 
<xsl:template match="/"> 
    <xsl:for-each select="$vallKeys"> 
    <xsl:value-of select= 
    "concat(position(),'.',.,'&#xA;')"/> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在下面的XML文檔(通過校正其嚴重malformedness從所提供的片段衍生的)施加:

<someParent> 
    <InterSection criteria="Microsoft-ASP">value</InterSection> 
    <InterSection criteria="Microsoft-MVC">value</InterSection> 
    <InterSection criteria="HP-MVC">value</InterSection> 
</someParent> 

有用,產生正確的結果

1.Microsoft 
2.HP